]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/tta.c
Merge remote-tracking branch 'shariman/wmall'
[ffmpeg] / libavcodec / tta.c
index 7ff5116ffbbb10efac3df7338b296ca3c4e84859..f6299018880b78a967e0849dd3e37e1cf2bded18 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;
@@ -232,6 +233,11 @@ static av_cold int tta_decode_init(AVCodecContext * avctx)
         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:
@@ -288,17 +294,19 @@ static av_cold int tta_decode_init(AVCodecContext * avctx)
         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, out_size;
+    int i, ret;
     int cur_chan = 0, framelen = s->frame_length;
     int32_t *p;
 
@@ -309,10 +317,11 @@ static int tta_decode_frame(AVCodecContext *avctx,
     if (!s->total_frames && s->last_frame_length)
         framelen = s->last_frame_length;
 
-    out_size = framelen * s->channels * av_get_bytes_per_sample(avctx->sample_fmt);
-    if (*data_size < out_size) {
-        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
@@ -409,20 +418,20 @@ static int tta_decode_frame(AVCodecContext *avctx,
         // convert to output buffer
         switch(s->bps) {
             case 1: {
-                uint8_t *samples = data;
+                uint8_t *samples = (uint8_t *)s->frame.data[0];
                 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
                     *samples++ = *p + 0x80;
                 break;
             }
             case 2: {
-                uint16_t *samples = data;
+                uint16_t *samples = (int16_t *)s->frame.data[0];
                 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
                     *samples++ = *p;
                 break;
             }
             case 3: {
                 // shift samples for 24-bit sample format
-                int32_t *samples = data;
+                int32_t *samples = (int32_t *)s->frame.data[0];
                 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
                     *samples++ <<= 8;
                 // reset decode buffer
@@ -433,7 +442,8 @@ static int tta_decode_frame(AVCodecContext *avctx,
                 av_log(s->avctx, AV_LOG_ERROR, "Error, only 16bit samples supported!\n");
         }
 
-    *data_size = out_size;
+    *got_frame_ptr   = 1;
+    *(AVFrame *)data = s->frame;
 
     return buf_size;
 }
@@ -455,5 +465,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)"),
 };