]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/svq1enc.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / svq1enc.c
index 914e9b64ca7da3d16d65baa98a8fc9061a002c11..08777957cf6f57d1c627f302476ae1b295e6a129 100644 (file)
@@ -2,20 +2,20 @@
  * SVQ1 Encoder
  * Copyright (C) 2004 Mike Melanson <melanson@pcisys.net>
  *
- * 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
  */
 
 #include "mpegvideo.h"
 #include "h263.h"
 #include "internal.h"
+#include "libavutil/avassert.h"
 #include "svq1.h"
 #include "svq1enc_cb.h"
 
-#undef NDEBUG
-#include <assert.h>
 
 typedef struct SVQ1Context {
     /* FIXME: Needed for motion estimation, should not be used for anything
@@ -94,7 +93,7 @@ static void svq1_write_header(SVQ1Context *s, int frame_type)
         /* output 5 unknown bits (2 + 2 + 1) */
         put_bits(&s->pb, 5, 2); /* 2 needed by quicktime decoder */
 
-        i = ff_match_2uint16(ff_svq1_frame_size_table,
+        i = ff_match_2uint16((void*)ff_svq1_frame_size_table,
                              FF_ARRAY_ELEMS(ff_svq1_frame_size_table),
                              s->frame_width, s->frame_height);
         put_bits(&s->pb, 3, i);
@@ -119,8 +118,8 @@ static int encode_block(SVQ1Context *s, uint8_t *src, uint8_t *ref,
     int count, y, x, i, j, split, best_mean, best_score, best_count;
     int best_vector[6];
     int block_sum[7] = { 0, 0, 0, 0, 0, 0 };
-    int w            = 2 << ((level + 2) >> 1);
-    int h            = 2 << ((level + 1) >> 1);
+    int w            = 2 << (level + 2 >> 1);
+    int h            = 2 << (level + 1 >> 1);
     int size         = w * h;
     int16_t block[7][256];
     const int8_t *codebook_sum, *codebook;
@@ -158,8 +157,8 @@ static int encode_block(SVQ1Context *s, uint8_t *src, uint8_t *ref,
     }
 
     best_count  = 0;
-    best_score -= (int)(((unsigned)block_sum[0] * block_sum[0]) >> (level + 3));
-    best_mean   = (block_sum[0] + (size >> 1)) >> (level + 3);
+    best_score -= (int)((unsigned)block_sum[0] * block_sum[0] >> (level + 3));
+    best_mean   = block_sum[0] + (size >> 1) >> (level + 3);
 
     if (level < 4) {
         for (count = 1; count < 7; count++) {
@@ -175,10 +174,10 @@ static int encode_block(SVQ1Context *s, uint8_t *src, uint8_t *ref,
                 vector = codebook + stage * size * 16 + i * size;
                 sqr    = s->dsp.ssd_int8_vs_int16(vector, block[stage], size);
                 diff   = block_sum[stage] - sum;
-                score  = sqr - ((diff * (int64_t)diff) >> (level + 3)); // FIXME: 64bit slooow
+                score  = sqr - (diff * (int64_t)diff >> (level + 3)); // FIXME: 64bit slooow
                 if (score < best_vector_score) {
-                    int mean = (diff + (size >> 1)) >> (level + 3);
-                    assert(mean > -300 && mean < 300);
+                    int mean = diff + (size >> 1) >> (level + 3);
+                    av_assert2(mean > -300 && mean < 300);
                     mean               = av_clip(mean, intra ? 0 : -256, 255);
                     best_vector_score  = score;
                     best_vector[stage] = i;
@@ -186,7 +185,7 @@ static int encode_block(SVQ1Context *s, uint8_t *src, uint8_t *ref,
                     best_vector_mean   = mean;
                 }
             }
-            assert(best_vector_mean != -999);
+            av_assert0(best_vector_mean != -999);
             vector = codebook + stage * size * 16 + best_vector[stage] * size;
             for (j = 0; j < size; j++)
                 block[stage + 1][j] = block[stage][j] - vector[j];
@@ -207,7 +206,7 @@ static int encode_block(SVQ1Context *s, uint8_t *src, uint8_t *ref,
     split = 0;
     if (best_score > threshold && level) {
         int score  = 0;
-        int offset = (level & 1) ? stride * h / 2 : w / 2;
+        int offset = level & 1 ? stride * h / 2 : w / 2;
         PutBitContext backup[6];
 
         for (i = level - 1; i >= 0; i--)
@@ -230,10 +229,10 @@ static int encode_block(SVQ1Context *s, uint8_t *src, uint8_t *ref,
         put_bits(&s->reorder_pb[level], 1, split);
 
     if (!split) {
-        assert((best_mean >= 0 && best_mean < 256) || !intra);
-        assert(best_mean >= -256 && best_mean < 256);
-        assert(best_count >= 0 && best_count < 7);
-        assert(level < 4 || best_count == 0);
+        av_assert1(best_mean >= 0 && best_mean < 256 || !intra);
+        av_assert1(best_mean >= -256 && best_mean < 256);
+        av_assert1(best_count >= 0 && best_count < 7);
+        av_assert1(level < 4 || best_count == 0);
 
         /* output the encoding */
         put_bits(&s->reorder_pb[level],
@@ -243,7 +242,7 @@ static int encode_block(SVQ1Context *s, uint8_t *src, uint8_t *ref,
                  mean_vlc[best_mean][0]);
 
         for (i = 0; i < best_count; i++) {
-            assert(best_vector[i] >= 0 && best_vector[i] < 16);
+            av_assert2(best_vector[i] >= 0 && best_vector[i] < 16);
             put_bits(&s->reorder_pb[level], 4, best_vector[i]);
         }
 
@@ -303,11 +302,11 @@ static int svq1_encode_plane(SVQ1Context *s, int plane,
         // s->m.out_format                    = FMT_H263;
         // s->m.unrestricted_mv               = 1;
         s->m.lambda                        = s->picture.quality;
-        s->m.qscale                        = (s->m.lambda * 139 +
-                                              FF_LAMBDA_SCALE * 64) >>
-                                             (FF_LAMBDA_SHIFT + 7);
-        s->m.lambda2                       = (s->m.lambda * s->m.lambda +
-                                              FF_LAMBDA_SCALE / 2) >>
+        s->m.qscale                        = s->m.lambda * 139 +
+                                             FF_LAMBDA_SCALE * 64 >>
+                                             FF_LAMBDA_SHIFT + 7;
+        s->m.lambda2                       = s->m.lambda * s->m.lambda +
+                                             FF_LAMBDA_SCALE / 2 >>
                                              FF_LAMBDA_SHIFT;
 
         if (!s->motion_val8[plane]) {
@@ -434,10 +433,10 @@ static int svq1_encode_plane(SVQ1Context *s, int plane,
                     s->m.pb = s->reorder_pb[5];
                     mx      = motion_ptr[0];
                     my      = motion_ptr[1];
-                    assert(mx     >= -32 && mx     <= 31);
-                    assert(my     >= -32 && my     <= 31);
-                    assert(pred_x >= -32 && pred_x <= 31);
-                    assert(pred_y >= -32 && pred_y <= 31);
+                    av_assert1(mx     >= -32 && mx     <= 31);
+                    av_assert1(my     >= -32 && my     <= 31);
+                    av_assert1(pred_x >= -32 && pred_x <= 31);
+                    av_assert1(pred_y >= -32 && pred_y <= 31);
                     ff_h263_encode_motion(&s->m, mx - pred_x, 1);
                     ff_h263_encode_motion(&s->m, my - pred_y, 1);
                     s->reorder_pb[5] = s->m.pb;
@@ -538,12 +537,9 @@ static int svq1_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     AVFrame temp;
     int i, ret;
 
-    if (!pkt->data &&
-        (ret = av_new_packet(pkt, s->y_block_width * s->y_block_height *
-                             MAX_MB_BYTES * 3 + FF_MIN_BUFFER_SIZE) < 0)) {
-        av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
+    if ((ret = ff_alloc_packet2(avctx, pkt, s->y_block_width * s->y_block_height *
+                             MAX_MB_BYTES*3 + FF_MIN_BUFFER_SIZE) < 0))
         return ret;
-    }
 
     if (avctx->pix_fmt != AV_PIX_FMT_YUV410P) {
         av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
@@ -613,6 +609,10 @@ static av_cold int svq1_encode_end(AVCodecContext *avctx)
         av_freep(&s->motion_val8[i]);
         av_freep(&s->motion_val16[i]);
     }
+    if(s->current_picture.data[0])
+        avctx->release_buffer(avctx, &s->current_picture);
+    if(s->last_picture.data[0])
+        avctx->release_buffer(avctx, &s->last_picture);
 
     return 0;
 }