From 7ead9d833bf7b250256d84f0da9661ba5b4f795c Mon Sep 17 00:00:00 2001
From: Aleksandr Slobodeniuk <alenuke@yandex.ru>
Date: Fri, 25 Aug 2017 17:02:18 +0300
Subject: [PATCH] libavformat/dv : change dv audio type to BE

---
 libavformat/dv.c      | 30 ++++++++++--------------------
 libavformat/dvenc.c   |  9 ++++-----
 libavformat/version.h |  2 +-
 3 files changed, 15 insertions(+), 26 deletions(-)

diff --git a/libavformat/dv.c b/libavformat/dv.c
index 06de044..d46b831 100644
--- a/libavformat/dv.c
+++ b/libavformat/dv.c
@@ -116,13 +116,13 @@ static const int dv_audio_frequency[3] = {
  * 3. Audio is always returned as 16-bit linear samples: 12-bit nonlinear samples
  *    are converted into 16-bit linear ones.
  */
-static int dv_extract_audio(const uint8_t *frame, uint8_t **ppcm,
+static int dv_extract_audio(const uint8_t *frame, uint16_t **ppcm,
                             const AVDVProfile *sys)
 {
     int size, chan, i, j, d, of, smpls, freq, quant, half_ch;
-    uint16_t lc, rc;
+    uint16_t lc, rc, *pcm;
     const uint8_t *as_pack;
-    uint8_t *pcm, ipcm;
+    uint8_t ipcm;
 
     as_pack = dv_extract_pack(frame, dv_audio_source);
     if (!as_pack)    /* No audio ? */
@@ -177,13 +177,9 @@ static int dv_extract_audio(const uint8_t *frame, uint8_t **ppcm,
                         if (of * 2 >= size)
                             continue;
 
-                        /* FIXME: maybe we have to admit that DV is a
-                         * big-endian PCM */
-                        pcm[of * 2]     = frame[d + 1];
-                        pcm[of * 2 + 1] = frame[d];
-
-                        if (pcm[of * 2 + 1] == 0x80 && pcm[of * 2] == 0x00)
-                            pcm[of * 2 + 1] = 0;
+                        pcm[of] = ((uint16_t*)frame)[d/2];
+                        if (pcm[of] == av_be2ne16(0x8000))
+                            pcm[of] = 0;
                     } else {           /* 12-bit quantization */
                         lc = ((uint16_t)frame[d]     << 4) |
                              ((uint16_t)frame[d + 2] >> 4);
@@ -197,16 +193,10 @@ static int dv_extract_audio(const uint8_t *frame, uint8_t **ppcm,
                         if (of * 2 >= size)
                             continue;
 
-                        /* FIXME: maybe we have to admit that DV is a
-                         * big-endian PCM */
-                        pcm[of * 2]     = lc & 0xff;
-                        pcm[of * 2 + 1] = lc >> 8;
+                        pcm[of] = lc;
                         of = sys->audio_shuffle[i % half_ch + half_ch][j] +
                              (d - 8) / 3 * sys->audio_stride;
-                        /* FIXME: maybe we have to admit that DV is a
-                         * big-endian PCM */
-                        pcm[of * 2]     = rc & 0xff;
-                        pcm[of * 2 + 1] = rc >> 8;
+                        pcm[of] = rc;
                         ++d;
                     }
                 }
@@ -260,7 +250,7 @@ static int dv_extract_audio_info(DVDemuxContext *c, const uint8_t *frame)
                 break;
             avpriv_set_pts_info(c->ast[i], 64, 1, 30000);
             c->ast[i]->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
-            c->ast[i]->codecpar->codec_id   = AV_CODEC_ID_PCM_S16LE;
+            c->ast[i]->codecpar->codec_id   = AV_CODEC_ID_PCM_S16BE;
 
             av_init_packet(&c->audio_pkt[i]);
             c->audio_pkt[i].size         = 0;
@@ -384,7 +374,7 @@ int avpriv_dv_produce_packet(DVDemuxContext *c, AVPacket *pkt,
         ppcm[i] = c->audio_buf[i];
     }
     if (c->ach)
-        dv_extract_audio(buf, ppcm, c->sys);
+        dv_extract_audio(buf, (uint16_t**)ppcm, c->sys);
 
     /* We work with 720p frames split in half, thus even frames have
      * channels 0,1 and odd 2,3. */
diff --git a/libavformat/dvenc.c b/libavformat/dvenc.c
index 93c103b..478c8d7 100644
--- a/libavformat/dvenc.c
+++ b/libavformat/dvenc.c
@@ -198,8 +198,7 @@ static void dv_inject_audio(DVMuxContext *c, int channel, uint8_t* frame_ptr)
                 if (of*2 >= size)
                     continue;
 
-                frame_ptr[d]   = *av_fifo_peek2(c->audio_data[channel], of*2+1); // FIXME: maybe we have to admit
-                frame_ptr[d+1] = *av_fifo_peek2(c->audio_data[channel], of*2);   //        that DV is a big-endian PCM
+                ((uint16_t*)frame_ptr)[d/2] = *(uint16_t*)av_fifo_peek2(c->audio_data[channel], of*2);
             }
             frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
         }
@@ -334,7 +333,7 @@ static DVMuxContext* dv_init_mux(AVFormatContext* s)
         goto bail_out;
     for (i=0; i<c->n_ast; i++) {
         if (c->ast[i]) {
-            if(c->ast[i]->codecpar->codec_id    != AV_CODEC_ID_PCM_S16LE ||
+            if(c->ast[i]->codecpar->codec_id    != AV_CODEC_ID_PCM_S16BE ||
                c->ast[i]->codecpar->channels    != 2)
                 goto bail_out;
             if (c->ast[i]->codecpar->sample_rate != 48000 &&
@@ -398,7 +397,7 @@ static int dv_write_header(AVFormatContext *s)
     if (!dv_init_mux(s)) {
         av_log(s, AV_LOG_ERROR, "Can't initialize DV format!\n"
                     "Make sure that you supply exactly two streams:\n"
-                    "     video: 25fps or 29.97fps, audio: 2ch/48|44|32kHz/PCM\n"
+                    "     video: 25fps or 29.97fps, audio: 2ch/48|44|32kHz/PCM S16 BE\n"
                     "     (50Mbps allows an optional second audio stream)\n");
         return -1;
     }
@@ -447,7 +446,7 @@ AVOutputFormat ff_dv_muxer = {
     .long_name         = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
     .extensions        = "dv",
     .priv_data_size    = sizeof(DVMuxContext),
-    .audio_codec       = AV_CODEC_ID_PCM_S16LE,
+    .audio_codec       = AV_CODEC_ID_PCM_S16BE,
     .video_codec       = AV_CODEC_ID_DVVIDEO,
     .write_header      = dv_write_header,
     .write_packet      = dv_write_packet,
diff --git a/libavformat/version.h b/libavformat/version.h
index 94081ac..bad3f20 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -33,7 +33,7 @@
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  57
 #define LIBAVFORMAT_VERSION_MINOR  78
-#define LIBAVFORMAT_VERSION_MICRO 100
+#define LIBAVFORMAT_VERSION_MICRO 101
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \
-- 
2.7.1.windows.1

