From 8ef40e72bcb6b82c76d593583be6bb977c362046 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 : read dv audio as BE

---
 libavformat/dv.c      | 30 ++++++++++--------------------
 libavformat/version.h |  2 +-
 2 files changed, 11 insertions(+), 21 deletions(-)

diff --git a/libavformat/dv.c b/libavformat/dv.c
index 06de044..890763e 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] = frame[d];
+                        if (pcm[of] == 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/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

