]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/tta.c
cabac: Mark ff_h264_mps_state array as static, it is only used within cabac.c.
[ffmpeg] / libavcodec / tta.c
index fa599b8828c171c896adbca8cea5508a7ae1b923..bc83bfdf1d3ba96999292694e8b3539bbac913a4 100644 (file)
@@ -27,7 +27,7 @@
  * @author Alex Beregszaszi
  */
 
-#define ALT_BITSTREAM_READER_LE
+#define BITSTREAM_READER_LE
 //#define DEBUG
 #include <limits.h>
 #include "avcodec.h"
@@ -56,6 +56,7 @@ typedef struct TTAChannel {
 
 typedef struct TTAContext {
     AVCodecContext *avctx;
+    AVFrame frame;
     GetBitContext gb;
 
     int format, channels, bps, data_length;
@@ -217,27 +218,34 @@ static av_cold int tta_decode_init(AVCodecContext * avctx)
         avctx->bits_per_coded_sample = get_bits(&s->gb, 16);
         s->bps = (avctx->bits_per_coded_sample + 7) / 8;
         avctx->sample_rate = get_bits_long(&s->gb, 32);
-        if(avctx->sample_rate > 1000000){ //prevent FRAME_TIME * avctx->sample_rate from overflowing and sanity check
-            av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
-            return -1;
-        }
         s->data_length = get_bits_long(&s->gb, 32);
         skip_bits(&s->gb, 32); // CRC32 of header
 
+        if (s->channels == 0) {
+            av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels\n");
+            return AVERROR_INVALIDDATA;
+        }
+
         switch(s->bps) {
-//            case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
-            case 2: avctx->sample_fmt = AV_SAMPLE_FMT_S16; break;
-//            case 3: avctx->sample_fmt = AV_SAMPLE_FMT_S24; break;
-            case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
-            default:
-                av_log_ask_for_sample(s->avctx,
-                                      "Invalid/unsupported sample format.\n");
-                return -1;
+        case 2:
+            avctx->sample_fmt = AV_SAMPLE_FMT_S16;
+            avctx->bits_per_raw_sample = 16;
+            break;
+        case 3:
+            avctx->sample_fmt = AV_SAMPLE_FMT_S32;
+            avctx->bits_per_raw_sample = 24;
+            break;
+        default:
+            av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
+            return AVERROR_INVALIDDATA;
         }
 
-        // FIXME: horribly broken, but directly from reference source
-#define FRAME_TIME 1.04489795918367346939
-        s->frame_length = (int)(FRAME_TIME * avctx->sample_rate);
+        // prevent overflow
+        if (avctx->sample_rate > 0x7FFFFF) {
+            av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
+            return AVERROR(EINVAL);
+        }
+        s->frame_length = 256 * avctx->sample_rate / 245;
 
         s->last_frame_length = s->data_length % s->frame_length;
         s->total_frames = s->data_length / s->frame_length +
@@ -259,26 +267,34 @@ static av_cold int tta_decode_init(AVCodecContext * avctx)
             return -1;
         }
 
-        s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
+        if (s->bps == 2) {
+            s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
+            if (!s->decode_buffer)
+                return AVERROR(ENOMEM);
+        }
         s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
-        if (!s->ch_ctx)
+        if (!s->ch_ctx) {
+            av_freep(&s->decode_buffer);
             return AVERROR(ENOMEM);
+        }
     } else {
         av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
         return -1;
     }
 
+    avcodec_get_frame_defaults(&s->frame);
+    avctx->coded_frame = &s->frame;
+
     return 0;
 }
 
-static int tta_decode_frame(AVCodecContext *avctx,
-        void *data, int *data_size,
-        AVPacket *avpkt)
+static int tta_decode_frame(AVCodecContext *avctx, void *data,
+                            int *got_frame_ptr, AVPacket *avpkt)
 {
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     TTAContext *s = avctx->priv_data;
-    int i;
+    int i, ret;
     int cur_chan = 0, framelen = s->frame_length;
     int32_t *p;
 
@@ -289,11 +305,17 @@ static int tta_decode_frame(AVCodecContext *avctx,
     if (!s->total_frames && s->last_frame_length)
         framelen = s->last_frame_length;
 
-    if (*data_size < (framelen * s->channels * 2)) {
-        av_log(avctx, AV_LOG_ERROR, "Output buffer size is too small.\n");
-        return -1;
+    /* get output buffer */
+    s->frame.nb_samples = framelen;
+    if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        return ret;
     }
 
+    // decode directly to output buffer for 24-bit sample format
+    if (s->bps == 3)
+        s->decode_buffer = s->frame.data[0];
+
     // init per channel states
     for (i = 0; i < s->channels; i++) {
         s->ch_ctx[i].predictor = 0;
@@ -382,19 +404,22 @@ static int tta_decode_frame(AVCodecContext *avctx,
     skip_bits(&s->gb, 32); // frame crc
 
     // convert to output buffer
-    switch(s->bps) {
-        case 2: {
-            uint16_t *samples = data;
-            for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
-                *samples++ = *p;
-            }
-            *data_size = (uint8_t *)samples - (uint8_t *)data;
-            break;
-        }
-        default:
-            av_log(s->avctx, AV_LOG_ERROR, "Error, only 16bit samples supported!\n");
+    if (s->bps == 2) {
+        int16_t *samples = (int16_t *)s->frame.data[0];
+        for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
+            *samples++ = *p;
+    } else {
+        // shift samples for 24-bit sample format
+        int32_t *samples = (int32_t *)s->frame.data[0];
+        for (i = 0; i < framelen * s->channels; i++)
+            *samples++ <<= 8;
+        // reset decode buffer
+        s->decode_buffer = NULL;
     }
 
+    *got_frame_ptr   = 1;
+    *(AVFrame *)data = s->frame;
+
     return buf_size;
 }
 
@@ -415,5 +440,6 @@ AVCodec ff_tta_decoder = {
     .init           = tta_decode_init,
     .close          = tta_decode_close,
     .decode         = tta_decode_frame,
+    .capabilities   = CODEC_CAP_DR1,
     .long_name = NULL_IF_CONFIG_SMALL("True Audio (TTA)"),
 };