]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/svq1dec.c
vc1dsp: K&R formatting cosmetics
[ffmpeg] / libavcodec / svq1dec.c
index 9c3c13a989f26027280f0e109f0571639487a3bb..000487b197d712844e1d133917919c37cbf56079 100644 (file)
@@ -33,8 +33,9 @@
  */
 
 #include "avcodec.h"
-#include "dsputil.h"
 #include "get_bits.h"
+#include "h263.h"
+#include "hpeldsp.h"
 #include "internal.h"
 #include "mathops.h"
 #include "svq1.h"
@@ -42,8 +43,6 @@
 #undef NDEBUG
 #include <assert.h>
 
-extern const uint8_t ff_mvtab[33][2];
-
 static VLC svq1_block_type;
 static VLC svq1_motion_component;
 static VLC svq1_intra_multistage[6];
@@ -58,9 +57,9 @@ typedef struct svq1_pmv_s {
 } svq1_pmv;
 
 typedef struct SVQ1Context {
-    DSPContext dsp;
+    HpelDSPContext hdsp;
     GetBitContext gb;
-    AVFrame *cur, *prev;
+    AVFrame *prev;
     int width;
     int height;
     int frame_code;
@@ -142,32 +141,6 @@ static const uint8_t string_table[256] = {
         n2 &= n3 & 0x00FF00FF;                                          \
     }
 
-#define SVQ1_DO_CODEBOOK_INTRA()                                        \
-    for (y = 0; y < height; y++) {                                      \
-        for (x = 0; x < width / 4; x++, codebook++) {                   \
-            n1 = n4;                                                    \
-            n2 = n4;                                                    \
-            SVQ1_ADD_CODEBOOK()                                         \
-            /* store result */                                          \
-            dst[x] = n1 << 8 | n2;                                      \
-        }                                                               \
-        dst += pitch / 4;                                               \
-    }
-
-#define SVQ1_DO_CODEBOOK_NONINTRA()                                     \
-    for (y = 0; y < height; y++) {                                      \
-        for (x = 0; x < width / 4; x++, codebook++) {                   \
-            n3 = dst[x];                                                \
-            /* add mean value to vector */                              \
-            n1 = n4 + ((n3 & 0xFF00FF00) >> 8);                         \
-            n2 = n4 +  (n3 & 0x00FF00FF);                               \
-            SVQ1_ADD_CODEBOOK()                                         \
-            /* store result */                                          \
-            dst[x] = n1 << 8 | n2;                                      \
-        }                                                               \
-        dst += pitch / 4;                                               \
-    }
-
 #define SVQ1_CALC_CODEBOOK_ENTRIES(cbook)                               \
     codebook = (const uint32_t *)cbook[level];                          \
     if (stages > 0)                                                     \
@@ -218,7 +191,7 @@ static int svq1_decode_block_intra(GetBitContext *bitbuf, uint8_t *pixels,
             av_dlog(NULL,
                     "Error (svq1_decode_block_intra): invalid vector: stages=%i level=%i\n",
                     stages, level);
-            return -1;  /* invalid vector */
+            return AVERROR_INVALIDDATA;  /* invalid vector */
         }
 
         mean = get_vlc2(bitbuf, svq1_intra_mean.table, 8, 3);
@@ -228,7 +201,17 @@ static int svq1_decode_block_intra(GetBitContext *bitbuf, uint8_t *pixels,
                 memset(&dst[y * (pitch / 4)], mean, width);
         } else {
             SVQ1_CALC_CODEBOOK_ENTRIES(ff_svq1_intra_codebooks);
-            SVQ1_DO_CODEBOOK_INTRA()
+
+            for (y = 0; y < height; y++) {
+                for (x = 0; x < width / 4; x++, codebook++) {
+                    n1 = n4;
+                    n2 = n4;
+                    SVQ1_ADD_CODEBOOK()
+                    /* store result */
+                    dst[x] = n1 << 8 | n2;
+                }
+                dst += pitch / 4;
+            }
         }
     }
 
@@ -270,13 +253,25 @@ static int svq1_decode_block_non_intra(GetBitContext *bitbuf, uint8_t *pixels,
             av_dlog(NULL,
                     "Error (svq1_decode_block_non_intra): invalid vector: stages=%i level=%i\n",
                     stages, level);
-            return -1;  /* invalid vector */
+            return AVERROR_INVALIDDATA;  /* invalid vector */
         }
 
         mean = get_vlc2(bitbuf, svq1_inter_mean.table, 9, 3) - 256;
 
         SVQ1_CALC_CODEBOOK_ENTRIES(ff_svq1_inter_codebooks);
-        SVQ1_DO_CODEBOOK_NONINTRA()
+
+        for (y = 0; y < height; y++) {
+            for (x = 0; x < width / 4; x++, codebook++) {
+                n3 = dst[x];
+                /* add mean value to vector */
+                n1 = n4 + ((n3 & 0xFF00FF00) >> 8);
+                n2 = n4 +  (n3 & 0x00FF00FF);
+                SVQ1_ADD_CODEBOOK()
+                /* store result */
+                dst[x] = n1 << 8 | n2;
+            }
+            dst += pitch / 4;
+        }
     }
     return 0;
 }
@@ -291,7 +286,7 @@ static int svq1_decode_motion_vector(GetBitContext *bitbuf, svq1_pmv *mv,
         /* get motion code */
         diff = get_vlc2(bitbuf, svq1_motion_component.table, 7, 2);
         if (diff < 0)
-            return -1;
+            return AVERROR_INVALIDDATA;
         else if (diff) {
             if (get_bits1(bitbuf))
                 diff = -diff;
@@ -324,9 +319,10 @@ static void svq1_skip_block(uint8_t *current, uint8_t *previous,
     }
 }
 
-static int svq1_motion_inter_block(DSPContext *dsp, GetBitContext *bitbuf,
+static int svq1_motion_inter_block(HpelDSPContext *hdsp, GetBitContext *bitbuf,
                                    uint8_t *current, uint8_t *previous,
-                                   int pitch, svq1_pmv *motion, int x, int y)
+                                   int pitch, svq1_pmv *motion, int x, int y,
+                                   int width, int height)
 {
     uint8_t *src;
     uint8_t *dst;
@@ -356,22 +352,21 @@ static int svq1_motion_inter_block(DSPContext *dsp, GetBitContext *bitbuf,
     motion[x / 8 + 2].y =
     motion[x / 8 + 3].y = mv.y;
 
-    if (y + (mv.y >> 1) < 0)
-        mv.y = 0;
-    if (x + (mv.x >> 1) < 0)
-        mv.x = 0;
+    mv.x = av_clip(mv.x, -2 * x, 2 * (width  - x - 16));
+    mv.y = av_clip(mv.y, -2 * y, 2 * (height - y - 16));
 
     src = &previous[(x + (mv.x >> 1)) + (y + (mv.y >> 1)) * pitch];
     dst = current;
 
-    dsp->put_pixels_tab[0][(mv.y & 1) << 1 | (mv.x & 1)](dst, src, pitch, 16);
+    hdsp->put_pixels_tab[0][(mv.y & 1) << 1 | (mv.x & 1)](dst, src, pitch, 16);
 
     return 0;
 }
 
-static int svq1_motion_inter_4v_block(DSPContext *dsp, GetBitContext *bitbuf,
+static int svq1_motion_inter_4v_block(HpelDSPContext *hdsp, GetBitContext *bitbuf,
                                       uint8_t *current, uint8_t *previous,
-                                      int pitch, svq1_pmv *motion, int x, int y)
+                                      int pitch, svq1_pmv *motion, int x, int y,
+                                      int width, int height)
 {
     uint8_t *src;
     uint8_t *dst;
@@ -431,15 +426,13 @@ static int svq1_motion_inter_4v_block(DSPContext *dsp, GetBitContext *bitbuf,
         int mvy = pmv[i]->y + (i >> 1) * 16;
 
         // FIXME: clipping or padding?
-        if (y + (mvy >> 1) < 0)
-            mvy = 0;
-        if (x + (mvx >> 1) < 0)
-            mvx = 0;
+        mvx = av_clip(mvx, -2 * x, 2 * (width  - x - 8));
+        mvy = av_clip(mvy, -2 * y, 2 * (height - y - 8));
 
         src = &previous[(x + (mvx >> 1)) + (y + (mvy >> 1)) * pitch];
         dst = current;
 
-        dsp->put_pixels_tab[1][((mvy & 1) << 1) | (mvx & 1)](dst, src, pitch, 8);
+        hdsp->put_pixels_tab[1][((mvy & 1) << 1) | (mvx & 1)](dst, src, pitch, 8);
 
         /* select next block */
         if (i & 1)
@@ -451,10 +444,11 @@ static int svq1_motion_inter_4v_block(DSPContext *dsp, GetBitContext *bitbuf,
     return 0;
 }
 
-static int svq1_decode_delta_block(AVCodecContext *avctx, DSPContext *dsp,
+static int svq1_decode_delta_block(AVCodecContext *avctx, HpelDSPContext *hdsp,
                                    GetBitContext *bitbuf,
                                    uint8_t *current, uint8_t *previous,
-                                   int pitch, svq1_pmv *motion, int x, int y)
+                                   int pitch, svq1_pmv *motion, int x, int y,
+                                   int width, int height)
 {
     uint32_t block_type;
     int result = 0;
@@ -478,8 +472,8 @@ static int svq1_decode_delta_block(AVCodecContext *avctx, DSPContext *dsp,
         break;
 
     case SVQ1_BLOCK_INTER:
-        result = svq1_motion_inter_block(dsp, bitbuf, current, previous,
-                                         pitch, motion, x, y);
+        result = svq1_motion_inter_block(hdsp, bitbuf, current, previous,
+                                         pitch, motion, x, y, width, height);
 
         if (result != 0) {
             av_dlog(avctx, "Error in svq1_motion_inter_block %i\n", result);
@@ -489,8 +483,8 @@ static int svq1_decode_delta_block(AVCodecContext *avctx, DSPContext *dsp,
         break;
 
     case SVQ1_BLOCK_INTER_4V:
-        result = svq1_motion_inter_4v_block(dsp, bitbuf, current, previous,
-                                            pitch, motion, x, y);
+        result = svq1_motion_inter_4v_block(hdsp, bitbuf, current, previous,
+                                            pitch, motion, x, y, width, height);
 
         if (result != 0) {
             av_dlog(avctx, "Error in svq1_motion_inter_4v_block %i\n", result);
@@ -580,11 +574,11 @@ static int svq1_decode_frame_header(AVCodecContext *avctx, AVFrame *frame)
             s->height = get_bits(bitbuf, 12);
 
             if (!s->width || !s->height)
-                return -1;
+                return AVERROR_INVALIDDATA;
         } else {
             /* get width, height from table */
-            s->width  = ff_svq1_frame_size_table[frame_size_code].width;
-            s->height = ff_svq1_frame_size_table[frame_size_code].height;
+            s->width  = ff_svq1_frame_size_table[frame_size_code][0];
+            s->height = ff_svq1_frame_size_table[frame_size_code][1];
         }
     }
 
@@ -594,7 +588,7 @@ static int svq1_decode_frame_header(AVCodecContext *avctx, AVFrame *frame)
         skip_bits1(bitbuf);    /* component checksums after image data if (1) */
 
         if (get_bits(bitbuf, 2) != 0)
-            return -1;
+            return AVERROR_INVALIDDATA;
     }
 
     if (get_bits1(bitbuf) == 1) {
@@ -616,14 +610,11 @@ static int svq1_decode_frame(AVCodecContext *avctx, void *data,
     const uint8_t *buf = avpkt->data;
     int buf_size       = avpkt->size;
     SVQ1Context     *s = avctx->priv_data;
-    AVFrame       *cur = s->cur;
+    AVFrame       *cur = data;
     uint8_t *current;
     int result, i, x, y, width, height;
     svq1_pmv *pmv;
 
-    if (cur->data[0])
-        avctx->release_buffer(avctx, cur);
-
     /* initialize bit buffer */
     init_get_bits(&s->gb, buf, buf_size * 8);
 
@@ -631,7 +622,7 @@ static int svq1_decode_frame(AVCodecContext *avctx, void *data,
     s->frame_code = get_bits(&s->gb, 22);
 
     if ((s->frame_code & ~0x70) || !(s->frame_code & 0x60))
-        return -1;
+        return AVERROR_INVALIDDATA;
 
     /* swap some header bytes (why?) */
     if (s->frame_code != 0x20) {
@@ -644,10 +635,13 @@ static int svq1_decode_frame(AVCodecContext *avctx, void *data,
     result = svq1_decode_frame_header(avctx, cur);
 
     if (result != 0) {
-        av_dlog(s->avctx, "Error in svq1_decode_frame_header %i\n", result);
+        av_dlog(avctx, "Error in svq1_decode_frame_header %i\n", result);
         return result;
     }
-    avcodec_set_dimensions(avctx, s->width, s->height);
+
+    result = ff_set_dimensions(avctx, s->width, s->height);
+    if (result < 0)
+        return result;
 
     if ((avctx->skip_frame >= AVDISCARD_NONREF && s->nonref) ||
         (avctx->skip_frame >= AVDISCARD_NONKEY &&
@@ -655,13 +649,13 @@ static int svq1_decode_frame(AVCodecContext *avctx, void *data,
         avctx->skip_frame >= AVDISCARD_ALL)
         return buf_size;
 
-    result = ff_get_buffer(avctx, cur);
+    result = ff_get_buffer(avctx, cur, s->nonref ? 0 : AV_GET_BUFFER_FLAG_REF);
     if (result < 0)
         return result;
 
     pmv = av_malloc((FFALIGN(s->width, 16) / 8 + 3) * sizeof(*pmv));
     if (!pmv)
-        return -1;
+        return AVERROR(ENOMEM);
 
     /* decode y, u and v components */
     for (i = 0; i < 3; i++) {
@@ -696,7 +690,8 @@ static int svq1_decode_frame(AVCodecContext *avctx, void *data,
         } else {
             /* delta frame */
             uint8_t *previous = s->prev->data[i];
-            if (!previous) {
+            if (!previous ||
+                s->prev->width != s->width || s->prev->height != s->height) {
                 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
                 result = AVERROR_INVALIDDATA;
                 goto err;
@@ -706,12 +701,12 @@ static int svq1_decode_frame(AVCodecContext *avctx, void *data,
 
             for (y = 0; y < height; y += 16) {
                 for (x = 0; x < width; x += 16) {
-                    result = svq1_decode_delta_block(avctx, &s->dsp,
+                    result = svq1_decode_delta_block(avctx, &s->hdsp,
                                                      &s->gb, &current[x],
                                                      previous, linesize,
-                                                     pmv, x, y);
+                                                     pmv, x, y, width, height);
                     if (result != 0) {
-                        av_dlog(s->avctx,
+                        av_dlog(avctx,
                                 "Error in svq1_decode_delta_block %i\n",
                                 result);
                         goto err;
@@ -726,9 +721,12 @@ static int svq1_decode_frame(AVCodecContext *avctx, void *data,
         }
     }
 
-    *(AVFrame*)data = *cur;
-    if (!s->nonref)
-        FFSWAP(AVFrame*, s->cur, s->prev);
+    if (!s->nonref) {
+        av_frame_unref(s->prev);
+        result = av_frame_ref(s->prev, cur);
+        if (result < 0)
+            goto err;
+    }
 
     *got_frame = 1;
     result     = buf_size;
@@ -744,19 +742,15 @@ static av_cold int svq1_decode_init(AVCodecContext *avctx)
     int i;
     int offset = 0;
 
-    s->cur  = avcodec_alloc_frame();
-    s->prev = avcodec_alloc_frame();
-    if (!s->cur || !s->prev) {
-        avcodec_free_frame(&s->cur);
-        avcodec_free_frame(&s->prev);
+    s->prev = av_frame_alloc();
+    if (!s->prev)
         return AVERROR(ENOMEM);
-    }
 
     s->width            = avctx->width  + 3 & ~3;
     s->height           = avctx->height + 3 & ~3;
     avctx->pix_fmt      = AV_PIX_FMT_YUV410P;
 
-    ff_dsputil_init(&s->dsp, avctx);
+    ff_hpeldsp_init(&s->hdsp, avctx->flags);
 
     INIT_VLC_STATIC(&svq1_block_type, 2, 4,
                     &ff_svq1_block_type_vlc[0][1], 2, 1,
@@ -801,12 +795,7 @@ static av_cold int svq1_decode_end(AVCodecContext *avctx)
 {
     SVQ1Context *s = avctx->priv_data;
 
-    if (s->cur->data[0])
-        avctx->release_buffer(avctx, s->cur);
-    if (s->prev->data[0])
-        avctx->release_buffer(avctx, s->prev);
-    avcodec_free_frame(&s->cur);
-    avcodec_free_frame(&s->prev);
+    av_frame_free(&s->prev);
 
     return 0;
 }
@@ -815,14 +804,12 @@ static void svq1_flush(AVCodecContext *avctx)
 {
     SVQ1Context *s = avctx->priv_data;
 
-    if (s->cur->data[0])
-        avctx->release_buffer(avctx, s->cur);
-    if (s->prev->data[0])
-        avctx->release_buffer(avctx, s->prev);
+    av_frame_unref(s->prev);
 }
 
 AVCodec ff_svq1_decoder = {
     .name           = "svq1",
+    .long_name      = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_SVQ1,
     .priv_data_size = sizeof(SVQ1Context),
@@ -831,7 +818,6 @@ AVCodec ff_svq1_decoder = {
     .decode         = svq1_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .flush          = svq1_flush,
-    .pix_fmts       = (const enum PixelFormat[]) { AV_PIX_FMT_YUV410P,
-                                                   AV_PIX_FMT_NONE },
-    .long_name      = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
+    .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV410P,
+                                                     AV_PIX_FMT_NONE },
 };