]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/adpcm.c
timecode: support >24h timecode.
[ffmpeg] / libavcodec / adpcm.c
index c88db5ca15ddd16fa70c3ae0c05de5cd59d3c59e..c176b5e03deaad3025b8ee47192d43ca1a27040a 100644 (file)
@@ -84,15 +84,20 @@ static const int swf_index_tables[4][16] = {
 /* end of tables */
 
 typedef struct ADPCMDecodeContext {
+    AVFrame frame;
     ADPCMChannelStatus status[6];
 } ADPCMDecodeContext;
 
 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
 {
     ADPCMDecodeContext *c = avctx->priv_data;
+    unsigned int min_channels = 1;
     unsigned int max_channels = 2;
 
     switch(avctx->codec->id) {
+    case CODEC_ID_ADPCM_EA:
+        min_channels = 2;
+        break;
     case CODEC_ID_ADPCM_EA_R1:
     case CODEC_ID_ADPCM_EA_R2:
     case CODEC_ID_ADPCM_EA_R3:
@@ -100,8 +105,9 @@ static av_cold int adpcm_decode_init(AVCodecContext * avctx)
         max_channels = 6;
         break;
     }
-    if(avctx->channels > max_channels){
-        return -1;
+    if (avctx->channels < min_channels || avctx->channels > max_channels) {
+        av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
+        return AVERROR(EINVAL);
     }
 
     switch(avctx->codec->id) {
@@ -124,6 +130,10 @@ static av_cold int adpcm_decode_init(AVCodecContext * avctx)
         break;
     }
     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
+
+    avcodec_get_frame_defaults(&c->frame);
+    avctx->coded_frame = &c->frame;
+
     return 0;
 }
 
@@ -335,6 +345,9 @@ static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf,
 
     *coded_samples = 0;
 
+    if(ch <= 0)
+        return 0;
+
     switch (avctx->codec->id) {
     /* constant, only check buf_size */
     case CODEC_ID_ADPCM_EA_XAS:
@@ -501,9 +514,8 @@ static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf,
         decode_top_nibble_next = 1; \
     }
 
-static int adpcm_decode_frame(AVCodecContext *avctx,
-                            void *data, int *data_size,
-                            AVPacket *avpkt)
+static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
+                              int *got_frame_ptr, AVPacket *avpkt)
 {
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
@@ -514,7 +526,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
     const uint8_t *src;
     int st; /* stereo */
     int count1, count2;
-    int nb_samples, coded_samples, out_bps, out_size;
+    int nb_samples, coded_samples, ret;
 
     nb_samples = get_nb_samples(avctx, buf, buf_size, &coded_samples);
     if (nb_samples <= 0) {
@@ -522,22 +534,22 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
         return AVERROR_INVALIDDATA;
     }
 
-    out_bps  = av_get_bytes_per_sample(avctx->sample_fmt);
-    out_size = nb_samples * avctx->channels * out_bps;
-    if (*data_size < out_size) {
-        av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
-        return AVERROR(EINVAL);
+    /* get output buffer */
+    c->frame.nb_samples = nb_samples;
+    if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        return ret;
     }
+    samples = (short *)c->frame.data[0];
+
     /* use coded_samples when applicable */
     /* it is always <= nb_samples, so the output buffer will be large enough */
     if (coded_samples) {
         if (coded_samples != nb_samples)
             av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
-        nb_samples = coded_samples;
-        out_size = nb_samples * avctx->channels * out_bps;
+        c->frame.nb_samples = nb_samples = coded_samples;
     }
 
-    samples = data;
     src = buf;
 
     st = avctx->channels == 2 ? 1 : 0;
@@ -576,7 +588,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
                 cs->step_index = 88;
             }
 
-            samples = (short*)data + channel;
+            samples = (short *)c->frame.data[0] + channel;
 
             for (m = 0; m < 32; m++) {
                 *samples = adpcm_ima_qt_expand_nibble(cs, src[0] & 0x0F, 3);
@@ -628,7 +640,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
         }
 
         for (i = 0; i < avctx->channels; i++) {
-            samples = (short*)data + i;
+            samples = (short *)c->frame.data[0] + i;
             cs = &c->status[i];
             for (n = nb_samples >> 1; n > 0; n--, src++) {
                 uint8_t v = *src;
@@ -810,6 +822,9 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
         /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
            each coding 28 stereo samples. */
 
+        if(avctx->channels != 2)
+            return AVERROR_INVALIDDATA;
+
         src += 4; // skip sample count (already read)
 
         current_left_sample   = (int16_t)bytestream_get_le16(&src);
@@ -824,13 +839,13 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
             coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
             src++;
 
-            shift_left  = (*src >> 4  ) + 8;
-            shift_right = (*src & 0x0F) + 8;
+            shift_left  = 20 - (*src >> 4);
+            shift_right = 20 - (*src & 0x0F);
             src++;
 
             for (count2 = 0; count2 < 28; count2++) {
-                next_left_sample  = (int32_t)((*src & 0xF0) << 24) >> shift_left;
-                next_right_sample = (int32_t)((*src & 0x0F) << 28) >> shift_right;
+                next_left_sample  = sign_extend(*src >> 4, 4) << shift_left;
+                next_right_sample = sign_extend(*src,      4) << shift_right;
                 src++;
 
                 next_left_sample = (next_left_sample +
@@ -861,13 +876,13 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
         for(channel = 0; channel < avctx->channels; channel++) {
             for (i=0; i<2; i++)
                 coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
-            shift[channel] = (*src & 0x0F) + 8;
+            shift[channel] = 20 - (*src & 0x0F);
             src++;
         }
         for (count1 = 0; count1 < nb_samples / 2; count1++) {
             for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
                 for(channel = 0; channel < avctx->channels; channel++) {
-                    int32_t sample = (int32_t)(((*(src+channel) >> i) & 0x0F) << 0x1C) >> shift[channel];
+                    int32_t sample = sign_extend(src[channel] >> i, 4) << shift[channel];
                     sample = (sample +
                              c->status[channel].sample1 * coeff[channel][0] +
                              c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
@@ -932,14 +947,14 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
                 } else {
                     coeff1 = ea_adpcm_table[ *srcC>>4     ];
                     coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
-                    shift = (*srcC++ & 0x0F) + 8;
+                    shift = 20 - (*srcC++ & 0x0F);
 
                     if (srcC > src_end - 14) break;
                     for (count2=0; count2<28; count2++) {
                         if (count2 & 1)
-                            next_sample = (int32_t)((*srcC++ & 0x0F) << 28) >> shift;
+                            next_sample = sign_extend(*srcC++,    4) << shift;
                         else
-                            next_sample = (int32_t)((*srcC   & 0xF0) << 24) >> shift;
+                            next_sample = sign_extend(*srcC >> 4, 4) << shift;
 
                         next_sample += (current_sample  * coeff1) +
                                        (previous_sample * coeff2);
@@ -965,7 +980,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
             }
         }
 
-        out_size = count * 28 * avctx->channels * out_bps;
+        c->frame.nb_samples = count * 28;
         src = src_end;
         break;
     }
@@ -976,7 +991,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
             for (n=0; n<4; n++, s+=32*avctx->channels) {
                 for (i=0; i<2; i++)
                     coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
-                shift[n] = (src[2]&0x0F) + 8;
+                shift[n] = 20 - (src[2] & 0x0F);
                 for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
                     s2[0] = (src[0]&0xF0) + (src[1]<<8);
             }
@@ -985,7 +1000,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
                 s = &samples[m*avctx->channels + channel];
                 for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
                     for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
-                        int level = (int32_t)((*src & (0xF0>>i)) << (24+i)) >> shift[n];
+                        int level = sign_extend(*src >> (4 - i), 4) << shift[n];
                         int pred  = s2[-1*avctx->channels] * coeff[0][n]
                                   + s2[-2*avctx->channels] * coeff[1][n];
                         s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
@@ -996,11 +1011,15 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
         break;
     case CODEC_ID_ADPCM_IMA_AMV:
     case CODEC_ID_ADPCM_IMA_SMJPEG:
-        c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
-        c->status[0].step_index = bytestream_get_le16(&src);
-
-        if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
-            src+=4;
+        if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV) {
+            c->status[0].predictor = sign_extend(bytestream_get_le16(&src), 16);
+            c->status[0].step_index = bytestream_get_le16(&src);
+            src += 4;
+        } else {
+            c->status[0].predictor = sign_extend(bytestream_get_be16(&src), 16);
+            c->status[0].step_index = bytestream_get_byte(&src);
+            src += 1;
+        }
 
         for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
             char hi, lo;
@@ -1144,23 +1163,23 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
             prev[0][i] = (int16_t)bytestream_get_be16(&src);
 
         for (ch = 0; ch <= st; ch++) {
-            samples = (unsigned short *) data + ch;
+            samples = (short *)c->frame.data[0] + ch;
 
             /* Read in every sample for this channel.  */
             for (i = 0; i < nb_samples / 14; i++) {
                 int index = (*src >> 4) & 7;
-                unsigned int exp = 28 - (*src++ & 15);
+                unsigned int exp = *src++ & 15;
                 int factor1 = table[ch][index * 2];
                 int factor2 = table[ch][index * 2 + 1];
 
                 /* Decode 14 samples.  */
                 for (n = 0; n < 14; n++) {
                     int32_t sampledat;
-                    if(n&1) sampledat=  *src++    <<28;
-                    else    sampledat= (*src&0xF0)<<24;
+                    if(n&1) sampledat = sign_extend(*src++, 4);
+                    else    sampledat = sign_extend(*src >> 4, 4);
 
                     sampledat = ((prev[ch][0]*factor1
-                                + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
+                                + prev[ch][1]*factor2) >> 11) + (sampledat << exp);
                     *samples = av_clip_int16(sampledat);
                     prev[ch][1] = prev[ch][0];
                     prev[ch][0] = *samples++;
@@ -1177,7 +1196,10 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
     default:
         return -1;
     }
-    *data_size = out_size;
+
+    *got_frame_ptr   = 1;
+    *(AVFrame *)data = c->frame;
+
     return src - buf;
 }
 
@@ -1190,6 +1212,7 @@ AVCodec ff_ ## name_ ## _decoder = {                        \
     .priv_data_size = sizeof(ADPCMDecodeContext),           \
     .init           = adpcm_decode_init,                    \
     .decode         = adpcm_decode_frame,                   \
+    .capabilities   = CODEC_CAP_DR1,                        \
     .long_name      = NULL_IF_CONFIG_SMALL(long_name_),     \
 }