Browse code

ADPCM AFC decoder

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

Paul B Mahol authored on 2012/11/19 19:51:44
Showing 10 changed files
... ...
@@ -755,6 +755,7 @@ following image formats are supported:
755 755
     @tab Used in some Sega Saturn console games.
756 756
 @item ADPCM Microsoft        @tab  X  @tab  X
757 757
 @item ADPCM MS IMA           @tab  X  @tab  X
758
+@item ADPCM Nintendo Gamecube AFC  @tab     @tab  X
758 759
 @item ADPCM Nintendo Gamecube THP  @tab     @tab  X
759 760
 @item ADPCM QT IMA           @tab  X  @tab  X
760 761
 @item ADPCM SEGA CRI ADX     @tab  X  @tab  X
... ...
@@ -566,6 +566,7 @@ OBJS-$(CONFIG_PCM_ZORK_DECODER)           += pcm.o
566 566
 OBJS-$(CONFIG_ADPCM_4XM_DECODER)          += adpcm.o adpcm_data.o
567 567
 OBJS-$(CONFIG_ADPCM_ADX_DECODER)          += adxdec.o adx.o
568 568
 OBJS-$(CONFIG_ADPCM_ADX_ENCODER)          += adxenc.o adx.o
569
+OBJS-$(CONFIG_ADPCM_AFC_DECODER)          += adpcm.o adpcm_data.o
569 570
 OBJS-$(CONFIG_ADPCM_CT_DECODER)           += adpcm.o adpcm_data.o
570 571
 OBJS-$(CONFIG_ADPCM_EA_DECODER)           += adpcm.o adpcm_data.o
571 572
 OBJS-$(CONFIG_ADPCM_EA_MAXIS_XA_DECODER)  += adpcm.o adpcm_data.o
... ...
@@ -145,6 +145,7 @@ static av_cold int adpcm_decode_init(AVCodecContext * avctx)
145 145
         case AV_CODEC_ID_ADPCM_EA_R3:
146 146
         case AV_CODEC_ID_ADPCM_EA_XAS:
147 147
         case AV_CODEC_ID_ADPCM_THP:
148
+        case AV_CODEC_ID_ADPCM_AFC:
148 149
             avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
149 150
             break;
150 151
         case AV_CODEC_ID_ADPCM_IMA_WS:
... ...
@@ -575,6 +576,9 @@ static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
575 575
         *coded_samples -= *coded_samples % 14;
576 576
         nb_samples      = (buf_size - 80) / (8 * ch) * 14;
577 577
         break;
578
+    case AV_CODEC_ID_ADPCM_AFC:
579
+        nb_samples = buf_size / (9 * ch) * 16;
580
+        break;
578 581
     case AV_CODEC_ID_ADPCM_XA:
579 582
         nb_samples = (buf_size / 128) * 224 / ch;
580 583
         break;
... ...
@@ -1232,6 +1236,44 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
1232 1232
             *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4  );
1233 1233
         }
1234 1234
         break;
1235
+    case AV_CODEC_ID_ADPCM_AFC:
1236
+        for (channel = 0; channel < avctx->channels; channel++) {
1237
+            int prev1 = c->status[channel].sample1;
1238
+            int prev2 = c->status[channel].sample2;
1239
+
1240
+            samples = samples_p[channel];
1241
+            /* Read in every sample for this channel.  */
1242
+            for (i = 0; i < nb_samples / 16; i++) {
1243
+                int byte = bytestream2_get_byteu(&gb);
1244
+                int scale = 1 << (byte >> 4);
1245
+                int index = byte & 0xf;
1246
+                int factor1 = ff_adpcm_afc_coeffs[0][index];
1247
+                int factor2 = ff_adpcm_afc_coeffs[1][index];
1248
+
1249
+                /* Decode 16 samples.  */
1250
+                for (n = 0; n < 16; n++) {
1251
+                    int32_t sampledat;
1252
+
1253
+                    if (n & 1) {
1254
+                        sampledat = sign_extend(byte, 4);
1255
+                    } else {
1256
+                        byte = bytestream2_get_byteu(&gb);
1257
+                        sampledat = sign_extend(byte >> 4, 4);
1258
+                    }
1259
+
1260
+                    sampledat = ((prev1 * factor1 + prev2 * factor2) +
1261
+                                 ((sampledat * scale) << 11)) >> 11;
1262
+                    *samples = av_clip_int16(sampledat);
1263
+                    prev2 = prev1;
1264
+                    prev1 = *samples++;
1265
+                }
1266
+            }
1267
+
1268
+            c->status[channel].sample1 = prev1;
1269
+            c->status[channel].sample2 = prev2;
1270
+        }
1271
+        bytestream2_seek(&gb, 0, SEEK_END);
1272
+        break;
1235 1273
     case AV_CODEC_ID_ADPCM_THP:
1236 1274
     {
1237 1275
         int table[2][16];
... ...
@@ -1314,6 +1356,7 @@ AVCodec ff_ ## name_ ## _decoder = {                        \
1314 1314
 
1315 1315
 /* Note: Do not forget to add new entries to the Makefile as well. */
1316 1316
 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM,         sample_fmts_s16p, adpcm_4xm,         "ADPCM 4X Movie");
1317
+ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC,         sample_fmts_s16p, adpcm_afc,         "ADPCM Nintendo Gamecube AFC");
1317 1318
 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT,          sample_fmts_s16,  adpcm_ct,          "ADPCM Creative Technology");
1318 1319
 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA,          sample_fmts_s16,  adpcm_ea,          "ADPCM Electronic Arts");
1319 1320
 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16,  adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
... ...
@@ -76,3 +76,8 @@ const int8_t ff_adpcm_yamaha_difflookup[] = {
76 76
      1,  3,  5,  7,  9,  11,  13,  15,
77 77
     -1, -3, -5, -7, -9, -11, -13, -15
78 78
 };
79
+
80
+const int16_t ff_adpcm_afc_coeffs[2][16] = {
81
+    { 0, 2048, 0, 1024, 4096, 3584, 3072, 4608, 4200, 4800, 5120, 2048, 1024, 64512, 64512, 63488 },
82
+    { 0, 0, 2048, 1024, 63488, 64000, 64512, 62976, 63288, 63236, 62464, 63488, 64512, 1024, 0, 0 }
83
+};
... ...
@@ -35,5 +35,6 @@ extern const uint8_t ff_adpcm_AdaptCoeff1[];
35 35
 extern const int8_t  ff_adpcm_AdaptCoeff2[];
36 36
 extern const int16_t ff_adpcm_yamaha_indexscale[];
37 37
 extern const int8_t  ff_adpcm_yamaha_difflookup[];
38
+extern const int16_t ff_adpcm_afc_coeffs[2][16];
38 39
 
39 40
 #endif /* AVCODEC_ADPCM_DATA_H */
... ...
@@ -387,6 +387,7 @@ void avcodec_register_all(void)
387 387
     /* ADPCM codecs */
388 388
     REGISTER_DECODER (ADPCM_4XM, adpcm_4xm);
389 389
     REGISTER_ENCDEC  (ADPCM_ADX, adpcm_adx);
390
+    REGISTER_DECODER (ADPCM_AFC, adpcm_afc);
390 391
     REGISTER_DECODER (ADPCM_CT, adpcm_ct);
391 392
     REGISTER_DECODER (ADPCM_EA, adpcm_ea);
392 393
     REGISTER_DECODER (ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa);
... ...
@@ -352,6 +352,7 @@ enum AVCodecID {
352 352
     AV_CODEC_ID_ADPCM_G722,
353 353
     AV_CODEC_ID_ADPCM_IMA_APC,
354 354
     AV_CODEC_ID_VIMA       = MKBETAG('V','I','M','A'),
355
+    AV_CODEC_ID_ADPCM_AFC  = MKBETAG('A','F','C',' '),
355 356
 
356 357
     /* AMR */
357 358
     AV_CODEC_ID_AMR_NB = 0x12000,
... ...
@@ -1756,6 +1756,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
1756 1756
         .long_name = NULL_IF_CONFIG_SMALL("ADPCM IMA CRYO APC"),
1757 1757
         .props     = AV_CODEC_PROP_LOSSY,
1758 1758
     },
1759
+    {
1760
+        .id        = AV_CODEC_ID_ADPCM_AFC,
1761
+        .type      = AVMEDIA_TYPE_AUDIO,
1762
+        .name      = "adpcm_afc",
1763
+        .long_name = NULL_IF_CONFIG_SMALL("ADPCM Nintendo Gamecube AFC"),
1764
+        .props     = AV_CODEC_PROP_LOSSY,
1765
+    },
1759 1766
 
1760 1767
     /* AMR */
1761 1768
     {
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR 54
32
-#define LIBAVCODEC_VERSION_MINOR 75
32
+#define LIBAVCODEC_VERSION_MINOR 76
33 33
 #define LIBAVCODEC_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -43,6 +43,9 @@ static int ast_read_header(AVFormatContext *s)
43 43
     avio_skip(s->pb, 8);
44 44
     codec = avio_rb16(s->pb);
45 45
     switch (codec) {
46
+    case 0:
47
+        st->codec->codec_id = AV_CODEC_ID_ADPCM_AFC;
48
+        break;
46 49
     case 1:
47 50
         st->codec->codec_id = AV_CODEC_ID_PCM_S16BE_PLANAR;
48 51
         break;