Browse code

avcodec: add Ulead DV audio decoder

Fixes #1564.

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2016/01/26 05:54:17
Showing 6 changed files
... ...
@@ -59,6 +59,7 @@ version <next>:
59 59
 - afftfilt filter
60 60
 - convolution filter
61 61
 - libquvi support removed
62
+- support for dvaudio in wav and avi
62 63
 
63 64
 
64 65
 version 2.8:
... ...
@@ -244,6 +244,7 @@ OBJS-$(CONFIG_DVBSUB_DECODER)          += dvbsubdec.o
244 244
 OBJS-$(CONFIG_DVBSUB_ENCODER)          += dvbsub.o
245 245
 OBJS-$(CONFIG_DVDSUB_DECODER)          += dvdsubdec.o
246 246
 OBJS-$(CONFIG_DVDSUB_ENCODER)          += dvdsubenc.o
247
+OBJS-$(CONFIG_DVAUDIO_DECODER)         += dvaudiodec.o
247 248
 OBJS-$(CONFIG_DVVIDEO_DECODER)         += dvdec.o dv.o dvdata.o
248 249
 OBJS-$(CONFIG_DVVIDEO_ENCODER)         += dvenc.o dv.o dvdata.o
249 250
 OBJS-$(CONFIG_DXA_DECODER)             += dxa.o
... ...
@@ -160,6 +160,7 @@ void avcodec_register_all(void)
160 160
     REGISTER_ENCDEC (DNXHD,             dnxhd);
161 161
     REGISTER_ENCDEC (DPX,               dpx);
162 162
     REGISTER_DECODER(DSICINVIDEO,       dsicinvideo);
163
+    REGISTER_DECODER(DVAUDIO,           dvaudio);
163 164
     REGISTER_ENCDEC (DVVIDEO,           dvvideo);
164 165
     REGISTER_DECODER(DXA,               dxa);
165 166
     REGISTER_DECODER(DXTORY,            dxtory);
166 167
new file mode 100644
... ...
@@ -0,0 +1,144 @@
0
+/*
1
+ * Copyright (c) 2012 Laurent Aimar
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "libavutil/intreadwrite.h"
21
+#include "avcodec.h"
22
+#include "internal.h"
23
+
24
+typedef struct DVAudioContext {
25
+    int block_size;
26
+    int is_12bit;
27
+    int is_pal;
28
+    int16_t shuffle[2000];
29
+} DVAudioContext;
30
+
31
+static av_cold int decode_init(AVCodecContext *avctx)
32
+{
33
+    DVAudioContext *s = avctx->priv_data;
34
+    int i;
35
+
36
+    if (avctx->channels > 2) {
37
+        av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
38
+        return AVERROR(EINVAL);
39
+    }
40
+
41
+    if (avctx->codec_tag == 0x0215) {
42
+        s->block_size = 7200;
43
+        s->is_pal = 0;
44
+    } else if (avctx->codec_tag == 0x0216) {
45
+        s->block_size = 8640;
46
+        s->is_pal = 1;
47
+    } else {
48
+        return AVERROR(EINVAL);
49
+    }
50
+
51
+    s->is_12bit = avctx->bits_per_raw_sample == 12;
52
+    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
53
+
54
+    for (i = 0; i < FF_ARRAY_ELEMS(s->shuffle); i++) {
55
+        const unsigned a = s->is_pal ? 18 : 15;
56
+        const unsigned b = 3 * a;
57
+
58
+        s->shuffle[i] = 80 * ((21 * (i % 3) + 9 * (i / 3) + ((i / a) % 3)) % b) +
59
+                         (2 + s->is_12bit) * (i / b) + 8;
60
+    }
61
+
62
+    return 0;
63
+}
64
+
65
+static inline int dv_get_audio_sample_count(const uint8_t *buffer, int dsf)
66
+{
67
+    int samples = buffer[0] & 0x3f; /* samples in this frame - min samples */
68
+
69
+    switch ((buffer[3] >> 3) & 0x07) {
70
+    case 0:
71
+        return samples + (dsf ? 1896 : 1580);
72
+    case 1:
73
+        return samples + (dsf ? 1742 : 1452);
74
+    case 2:
75
+    default:
76
+        return samples + (dsf ? 1264 : 1053);
77
+    }
78
+}
79
+
80
+static inline uint16_t dv_audio_12to16(uint16_t sample)
81
+{
82
+    uint16_t shift, result;
83
+
84
+    sample = (sample < 0x800) ? sample : sample | 0xf000;
85
+    shift  = (sample & 0xf00) >> 8;
86
+
87
+    if (shift < 0x2 || shift > 0xd) {
88
+        result = sample;
89
+    } else if (shift < 0x8) {
90
+        shift--;
91
+        result = (sample - (256 * shift)) << shift;
92
+    } else {
93
+        shift  = 0xe - shift;
94
+        result = ((sample + ((256 * shift) + 1)) << shift) - 1;
95
+    }
96
+
97
+    return result;
98
+}
99
+
100
+static int decode_frame(AVCodecContext *avctx, void *data,
101
+                        int *got_frame_ptr, AVPacket *pkt)
102
+{
103
+    DVAudioContext *s = avctx->priv_data;
104
+    AVFrame *frame = data;
105
+    const uint8_t *src = pkt->data;
106
+    int16_t *dst;
107
+    int ret, i;
108
+
109
+    if (pkt->size != s->block_size)
110
+        return AVERROR_INVALIDDATA;
111
+
112
+    frame->nb_samples = dv_get_audio_sample_count(pkt->data + 244, s->is_pal);
113
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
114
+        return ret;
115
+    dst = (int16_t *)frame->data[0];
116
+
117
+    for (i = 0; i < frame->nb_samples; i++) {
118
+       const uint8_t *v = &src[s->shuffle[i]];
119
+
120
+       if (s->is_12bit) {
121
+           *dst++ = dv_audio_12to16((v[0] << 4) | ((v[2] >> 4) & 0x0f));
122
+           *dst++ = dv_audio_12to16((v[1] << 4) | ((v[2] >> 0) & 0x0f));
123
+       } else {
124
+           *dst++ = AV_RB16(&v[0]);
125
+           *dst++ = AV_RB16(&v[s->is_pal ? 4320 : 3600]);
126
+       }
127
+    }
128
+
129
+    *got_frame_ptr = 1;
130
+
131
+    return pkt->size;
132
+}
133
+
134
+AVCodec ff_dvaudio_decoder = {
135
+    .name           = "dvaudio",
136
+    .long_name      = NULL_IF_CONFIG_SMALL("Ulead DV Audio"),
137
+    .type           = AVMEDIA_TYPE_AUDIO,
138
+    .id             = AV_CODEC_ID_DVAUDIO,
139
+    .init           = decode_init,
140
+    .decode         = decode_frame,
141
+    .capabilities   = AV_CODEC_CAP_DR1,
142
+    .priv_data_size = sizeof(DVAudioContext),
143
+};
... ...
@@ -30,7 +30,7 @@
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR  57
32 32
 #define LIBAVCODEC_VERSION_MINOR  22
33
-#define LIBAVCODEC_VERSION_MICRO 101
33
+#define LIBAVCODEC_VERSION_MICRO 102
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
36 36
                                                LIBAVCODEC_VERSION_MINOR, \
... ...
@@ -463,6 +463,8 @@ const AVCodecTag ff_codec_wav_tags[] = {
463 463
     { AV_CODEC_ID_XMA1,            0x0165 },
464 464
     { AV_CODEC_ID_XMA2,            0x0166 },
465 465
     { AV_CODEC_ID_ADPCM_CT,        0x0200 },
466
+    { AV_CODEC_ID_DVAUDIO,         0x0215 },
467
+    { AV_CODEC_ID_DVAUDIO,         0x0216 },
466 468
     { AV_CODEC_ID_ATRAC3,          0x0270 },
467 469
     { AV_CODEC_ID_ADPCM_G722,      0x028F },
468 470
     { AV_CODEC_ID_IMC,             0x0401 },