]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/imc.c
Support reading 64bit sgi images.
[ffmpeg] / libavcodec / imc.c
index d0a0d9da022636349219ea932e57cdaaebae7def..d3b8bf5a125a58e09381a3f7f0d184eb2464cc9a 100644 (file)
@@ -4,20 +4,20 @@
  * Copyright (c) 2006 Benjamin Larsson
  * Copyright (c) 2006 Konstantin Shishkov
  *
- * This file is part of Libav.
+ * This file is part of FFmpeg.
  *
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
@@ -35,7 +35,6 @@
 #include <stddef.h>
 #include <stdio.h>
 
-#define ALT_BITSTREAM_READER
 #include "avcodec.h"
 #include "get_bits.h"
 #include "dsputil.h"
@@ -51,6 +50,8 @@
 #define COEFFS 256
 
 typedef struct {
+    AVFrame frame;
+
     float old_floor[BANDS];
     float flcoeffs1[BANDS];
     float flcoeffs2[BANDS];
@@ -167,7 +168,11 @@ static av_cold int imc_decode_init(AVCodecContext * avctx)
     }
     dsputil_init(&q->dsp, avctx);
     avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
-    avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
+    avctx->channel_layout = AV_CH_LAYOUT_MONO;
+
+    avcodec_get_frame_defaults(&q->frame);
+    avctx->coded_frame = &q->frame;
+
     return 0;
 }
 
@@ -344,7 +349,7 @@ static int bit_allocation (IMCContext* q, int stream_format_code, int freebits,
             indx = 2;
 
         if (indx == -1)
-            return -1;
+            return AVERROR_INVALIDDATA;
 
         q->flcoeffs4[i] = q->flcoeffs4[i] + xTab[(indx*2 + (q->flcoeffs1[i] < highest)) * 2 + flag];
     }
@@ -603,7 +608,7 @@ static int inverse_quant_coeff (IMCContext* q, int stream_format_code) {
             middle_value = max_size >> 1;
 
             if (q->codewords[j] >= max_size || q->codewords[j] < 0)
-                return -1;
+                return AVERROR_INVALIDDATA;
 
             if (cw_len >= 4){
                 quantizer = imc_quantizer2[(stream_format_code & 2) >> 1];
@@ -636,7 +641,7 @@ static int imc_get_coeffs (IMCContext* q) {
 
                 if (get_bits_count(&q->gb) + cw_len > 512){
 //av_log(NULL,0,"Band %i coeff %i cw_len %i\n",i,j,cw_len);
-                    return -1;
+                    return AVERROR_INVALIDDATA;
                 }
 
                 if(cw_len && (!q->bandFlagsBuf[i] || !q->skipFlags[j]))
@@ -649,9 +654,8 @@ static int imc_get_coeffs (IMCContext* q) {
     return 0;
 }
 
-static int imc_decode_frame(AVCodecContext * avctx,
-                            void *data, int *data_size,
-                            AVPacket *avpkt)
+static int imc_decode_frame(AVCodecContext * avctx, void *data,
+                            int *got_frame_ptr, AVPacket *avpkt)
 {
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
@@ -659,7 +663,7 @@ static int imc_decode_frame(AVCodecContext * avctx,
     IMCContext *q = avctx->priv_data;
 
     int stream_format_code;
-    int imc_hdr, i, j, out_size;
+    int imc_hdr, i, j, ret;
     int flag;
     int bits, summer;
     int counter, bitscount;
@@ -667,18 +671,19 @@ static int imc_decode_frame(AVCodecContext * avctx,
 
     if (buf_size < IMC_BLOCK_SIZE) {
         av_log(avctx, AV_LOG_ERROR, "imc frame too small!\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
-    out_size = COEFFS * av_get_bytes_per_sample(avctx->sample_fmt);
-    if (*data_size < out_size) {
-        av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n");
-        return AVERROR(EINVAL);
+    /* get output buffer */
+    q->frame.nb_samples = COEFFS;
+    if ((ret = avctx->get_buffer(avctx, &q->frame)) < 0) {
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        return ret;
     }
+    q->out_samples = (float *)q->frame.data[0];
 
     q->dsp.bswap16_buf(buf16, (const uint16_t*)buf, IMC_BLOCK_SIZE / 2);
 
-    q->out_samples = data;
     init_get_bits(&q->gb, (const uint8_t*)buf16, IMC_BLOCK_SIZE * 8);
 
     /* Check the frame header */
@@ -686,13 +691,13 @@ static int imc_decode_frame(AVCodecContext * avctx,
     if (imc_hdr != IMC_FRAME_ID) {
         av_log(avctx, AV_LOG_ERROR, "imc frame header check failed!\n");
         av_log(avctx, AV_LOG_ERROR, "got %x instead of 0x21.\n", imc_hdr);
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     stream_format_code = get_bits(&q->gb, 3);
 
     if(stream_format_code & 1){
         av_log(avctx, AV_LOG_ERROR, "Stream code format %X is not supported\n", stream_format_code);
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
 //    av_log(avctx, AV_LOG_DEBUG, "stream_format_code = %d\n", stream_format_code);
@@ -752,10 +757,11 @@ static int imc_decode_frame(AVCodecContext * avctx,
         }
     }
 
-    if(bit_allocation (q, stream_format_code, 512 - bitscount - get_bits_count(&q->gb), flag) < 0) {
+    if((ret = bit_allocation (q, stream_format_code,
+                              512 - bitscount - get_bits_count(&q->gb), flag)) < 0) {
         av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n");
         q->decoder_reset = 1;
-        return -1;
+        return ret;
     }
 
     for(i = 0; i < BANDS; i++) {
@@ -809,20 +815,21 @@ static int imc_decode_frame(AVCodecContext * avctx,
     if(imc_get_coeffs(q) < 0) {
         av_log(avctx, AV_LOG_ERROR, "Read coefficients failed\n");
         q->decoder_reset = 1;
-        return 0;
+        return AVERROR_INVALIDDATA;
     }
 
     if(inverse_quant_coeff(q, stream_format_code) < 0) {
         av_log(avctx, AV_LOG_ERROR, "Inverse quantization of coefficients failed\n");
         q->decoder_reset = 1;
-        return 0;
+        return AVERROR_INVALIDDATA;
     }
 
     memset(q->skipFlags, 0, sizeof(q->skipFlags));
 
     imc_imdct256(q);
 
-    *data_size = out_size;
+    *got_frame_ptr   = 1;
+    *(AVFrame *)data = q->frame;
 
     return IMC_BLOCK_SIZE;
 }
@@ -833,6 +840,7 @@ static av_cold int imc_decode_close(AVCodecContext * avctx)
     IMCContext *q = avctx->priv_data;
 
     ff_fft_end(&q->fft);
+
     return 0;
 }
 
@@ -845,5 +853,6 @@ AVCodec ff_imc_decoder = {
     .init = imc_decode_init,
     .close = imc_decode_close,
     .decode = imc_decode_frame,
+    .capabilities = CODEC_CAP_DR1,
     .long_name = NULL_IF_CONFIG_SMALL("IMC (Intel Music Coder)"),
 };