Revision d1e0d21f

View differences:

doc/ffmpeg-doc.texi
902 902
@tab This format is used in non-Windows version of Feeble Files game and
903 903
different game cutscenes repacked for use with ScummVM.
904 904
@item THP @tab    @tab X
905
@tab Used on the Nintendo GameCube (video only)
905
@tab Used on the Nintendo GameCube.
906 906
@item C93 @tab    @tab X
907 907
@tab Used in the game Cyberia from Interplay.
908 908
@end multitable
libavcodec/Makefile
250 250
OBJS-$(CONFIG_ADPCM_SBPRO_4_ENCODER)   += adpcm.o
251 251
OBJS-$(CONFIG_ADPCM_SWF_DECODER)       += adpcm.o
252 252
OBJS-$(CONFIG_ADPCM_SWF_ENCODER)       += adpcm.o
253
OBJS-$(CONFIG_ADPCM_THP_DECODER)       += adpcm.o
253 254
OBJS-$(CONFIG_ADPCM_XA_DECODER)        += adpcm.o
254 255
OBJS-$(CONFIG_ADPCM_XA_ENCODER)        += adpcm.o
255 256
OBJS-$(CONFIG_ADPCM_YAMAHA_DECODER)    += adpcm.o
libavcodec/adpcm.c
29 29
 *   by Mike Melanson (melanson@pcisys.net)
30 30
 * CD-ROM XA ADPCM codec by BERO
31 31
 * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
32
 * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
32 33
 *
33 34
 * Features and limitations:
34 35
 *
......
1308 1309
            src++;
1309 1310
        }
1310 1311
        break;
1312
    case CODEC_ID_ADPCM_THP:
1313
      {
1314
        GetBitContext gb;
1315
        int table[16][2];
1316
        unsigned int samplecnt;
1317
        int prev1[2], prev2[2];
1318
        int ch;
1319

  
1320
        if (buf_size < 80) {
1321
            av_log(avctx, AV_LOG_ERROR, "frame too small\n");
1322
            return -1;
1323
        }
1324

  
1325
        init_get_bits(&gb, src, buf_size * 8);
1326
        src += buf_size;
1327

  
1328
                    get_bits_long(&gb, 32); /* Channel size */
1329
        samplecnt = get_bits_long(&gb, 32);
1330

  
1331
        for (ch = 0; ch < 2; ch++)
1332
            for (i = 0; i < 16; i++)
1333
                table[i][ch] = get_sbits(&gb, 16);
1334

  
1335
        /* Initialize the previous sample.  */
1336
        for (ch = 0; ch < 2; ch++) {
1337
            prev1[ch] = get_sbits(&gb, 16);
1338
            prev2[ch] = get_sbits(&gb, 16);
1339
        }
1340

  
1341
        if (samplecnt >= (samples_end - samples) /  (st + 1)) {
1342
            av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
1343
            return -1;
1344
        }
1345

  
1346
        for (ch = 0; ch <= st; ch++) {
1347
            samples = (unsigned short *) data + ch;
1348

  
1349
            /* Read in every sample for this channel.  */
1350
            for (i = 0; i < samplecnt / 14; i++) {
1351
                uint8_t index = get_bits (&gb, 4) & 7;
1352
                unsigned int exp = get_bits (&gb, 4);
1353
                int factor1 = table[index * 2][ch];
1354
                int factor2 = table[index * 2 + 1][ch];
1355

  
1356
                /* Decode 14 samples.  */
1357
                for (n = 0; n < 14; n++) {
1358
                    int sampledat = get_sbits (&gb, 4);
1359

  
1360
                    *samples = ((prev1[ch]*factor1
1361
                                + prev2[ch]*factor2) >> 11) + (sampledat << exp);
1362
                    prev2[ch] = prev1[ch];
1363
                    prev1[ch] = *samples++;
1364

  
1365
                    /* In case of stereo, skip one sample, this sample
1366
                       is for the other channel.  */
1367
                    samples += st;
1368
                }
1369
            }
1370
        }
1371

  
1372
        /* In the previous loop, in case stereo is used, samples is
1373
           increased exactly one time too often.  */
1374
        samples -= st;
1375
        break;
1376
      }
1377

  
1311 1378
    default:
1312 1379
        return -1;
1313 1380
    }
......
1368 1435
ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
1369 1436
ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
1370 1437
ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);
1438
ADPCM_CODEC(CODEC_ID_ADPCM_THP, adpcm_thp);
1371 1439

  
1372 1440
#undef ADPCM_CODEC
libavcodec/allcodecs.c
244 244
    REGISTER_ENCDEC (ADPCM_SBPRO_3, adpcm_sbpro_3);
245 245
    REGISTER_ENCDEC (ADPCM_SBPRO_4, adpcm_sbpro_4);
246 246
    REGISTER_ENCDEC (ADPCM_SWF, adpcm_swf);
247
    REGISTER_DECODER(ADPCM_THP, adpcm_thp);
247 248
    REGISTER_ENCDEC (ADPCM_XA, adpcm_xa);
248 249
    REGISTER_ENCDEC (ADPCM_YAMAHA, adpcm_yamaha);
249 250

  
libavcodec/avcodec.h
200 200
    CODEC_ID_ADPCM_SBPRO_4,
201 201
    CODEC_ID_ADPCM_SBPRO_3,
202 202
    CODEC_ID_ADPCM_SBPRO_2,
203
    CODEC_ID_ADPCM_THP,
203 204

  
204 205
    /* AMR */
205 206
    CODEC_ID_AMR_NB= 0x12000,
......
2417 2418
PCM_CODEC(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
2418 2419
PCM_CODEC(CODEC_ID_ADPCM_SMJPEG,  adpcm_ima_smjpeg);
2419 2420
PCM_CODEC(CODEC_ID_ADPCM_SWF,     adpcm_swf);
2421
PCM_CODEC(CODEC_ID_ADPCM_THP,     adpcm_thp);
2420 2422
PCM_CODEC(CODEC_ID_ADPCM_XA,      adpcm_xa);
2421 2423
PCM_CODEC(CODEC_ID_ADPCM_YAMAHA,  adpcm_yamaha);
2422 2424

  
libavformat/thp.c
35 35
    int              next_frame;
36 36
    int              next_framesz;
37 37
    int              video_stream_index;
38
    int              audio_stream_index;
38 39
    int              compcount;
39 40
    unsigned char    components[16];
40 41
    AVStream*        vst;
41 42
    int              has_audio;
43
    int              audiosize;
42 44
} ThpDemuxContext;
43 45

  
44 46

  
......
116 118
             get_be32(pb); /* Unknown.  */
117 119
        }
118 120
      else if (thp->components[i] == 1) {
119
          /* XXX: Required for audio playback.  */
121
          if (thp->has_audio != 0)
122
              break;
123

  
124
          /* Audio component.  */
125
          st = av_new_stream(s, 0);
126
          if (!st)
127
              return AVERROR_NOMEM;
128

  
129
          st->codec->codec_type = CODEC_TYPE_AUDIO;
130
          st->codec->codec_id = CODEC_ID_ADPCM_THP;
131
          st->codec->codec_tag = 0;  /* no fourcc */
132
          st->codec->channels    = get_be32(pb); /* numChannels.  */
133
          st->codec->sample_rate = get_be32(pb); /* Frequency.  */
134

  
135
          av_set_pts_info(st, 64, 1, st->codec->sample_rate);
136

  
137
          thp->audio_stream_index = st->index;
120 138
          thp->has_audio = 1;
121 139
      }
122 140
    }
......
132 150
    int size;
133 151
    int ret;
134 152

  
153
    if (thp->audiosize == 0) {
154

  
135 155
    /* Terminate when last frame is reached.  */
136 156
    if (thp->frame >= thp->framecnt)
137 157
       return AVERROR_IO;
......
145 165
                        get_be32(pb); /* Previous total size.  */
146 166
    size              = get_be32(pb); /* Total size of this frame.  */
147 167

  
168
    /* Store the audiosize so the next time this function is called,
169
       the audio can be read.  */
148 170
    if (thp->has_audio)
149
                        get_be32(pb); /* Audio size.  */
171
        thp->audiosize = get_be32(pb); /* Audio size.  */
172
    else
173
        thp->frame++;
150 174

  
151 175
    ret = av_get_packet(pb, pkt, size);
152 176
    if (ret != size) {
......
155 179
    }
156 180

  
157 181
    pkt->stream_index = thp->video_stream_index;
158
    thp->frame++;
182
    }
183
    else {
184
        ret = av_get_packet(pb, pkt, thp->audiosize);
185
        if (ret != thp->audiosize) {
186
            av_free_packet(pkt);
187
            return AVERROR_IO;
188
        }
189

  
190
        pkt->stream_index = thp->audio_stream_index;
191
        thp->audiosize = 0;
192
        thp->frame++;
193
    }
159 194

  
160 195
    return 0;
161 196
}

Also available in: Unified diff