[FFmpeg-cvslog] avcodec: use av_mod_uintp2() where useful

James Almer git at videolan.org
Wed Apr 22 03:45:59 CEST 2015


ffmpeg | branch: master | James Almer <jamrial at gmail.com> | Tue Apr 21 22:28:21 2015 -0300| [ba625dd8a12b8f440af7f50c833e5c1005d67c85] | committer: James Almer

avcodec: use av_mod_uintp2() where useful

Reviewed-by: Michael Niedermayer <michaelni at gmx.at>
Signed-off-by: James Almer <jamrial at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=ba625dd8a12b8f440af7f50c833e5c1005d67c85
---

 libavcodec/adpcm.c             |    2 +-
 libavcodec/atrac3plus.c        |    4 ++--
 libavcodec/dnxhdenc.c          |    2 +-
 libavcodec/dvenc.c             |    2 +-
 libavcodec/ffv1.h              |    2 +-
 libavcodec/ffv1dec.c           |    3 +--
 libavcodec/g726.c              |    2 +-
 libavcodec/g729dec.c           |    2 +-
 libavcodec/golomb.h            |    2 +-
 libavcodec/h264.c              |    5 ++---
 libavcodec/h264_refs.c         |    2 +-
 libavcodec/hevc.c              |   16 ++++++++--------
 libavcodec/hevc_cabac.c        |    8 ++++----
 libavcodec/hevc_mvs.c          |    4 ++--
 libavcodec/hevc_ps.c           |    4 ++--
 libavcodec/hevcpred_template.c |    4 ++--
 libavcodec/mpeg12enc.c         |    8 ++++----
 libavcodec/opus.h              |    2 +-
 libavcodec/opus_celt.c         |    4 ++--
 libavcodec/proresenc_kostya.c  |    6 ++----
 libavcodec/put_bits.h          |    2 +-
 21 files changed, 41 insertions(+), 45 deletions(-)

diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c
index 56d1660..251ed1d 100644
--- a/libavcodec/adpcm.c
+++ b/libavcodec/adpcm.c
@@ -197,7 +197,7 @@ static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitC
     step_index = av_clip(step_index, 0, 88);
 
     sign = nibble & (1 << shift);
-    delta = nibble & ((1 << shift) - 1);
+    delta = av_mod_uintp2(nibble, shift);
     diff = ((2 * delta + 1) * step) >> shift;
     predictor = c->predictor;
     if (sign) predictor -= diff;
diff --git a/libavcodec/atrac3plus.c b/libavcodec/atrac3plus.c
index 575a493..f998a7f 100644
--- a/libavcodec/atrac3plus.c
+++ b/libavcodec/atrac3plus.c
@@ -820,7 +820,7 @@ static void decode_qu_spectra(GetBitContext *gb, const Atrac3pSpecCodeTab *tab,
     int num_coeffs = tab->num_coeffs;
     int bits       = tab->bits;
     int is_signed  = tab->is_signed;
-    unsigned val, mask = (1 << bits) - 1;
+    unsigned val;
 
     for (pos = 0; pos < num_specs;) {
         if (group_size == 1 || get_bits1(gb)) {
@@ -828,7 +828,7 @@ static void decode_qu_spectra(GetBitContext *gb, const Atrac3pSpecCodeTab *tab,
                 val = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
 
                 for (i = 0; i < num_coeffs; i++) {
-                    cf = val & mask;
+                    cf = av_mod_uintp2(val, bits);
                     if (is_signed)
                         cf = sign_extend(cf, bits);
                     else if (cf && get_bits1(gb))
diff --git a/libavcodec/dnxhdenc.c b/libavcodec/dnxhdenc.c
index 005602e..90d51ff 100644
--- a/libavcodec/dnxhdenc.c
+++ b/libavcodec/dnxhdenc.c
@@ -450,7 +450,7 @@ static av_always_inline void dnxhd_encode_dc(DNXHDEncContext *ctx, int diff)
     }
     put_bits(&ctx->m.pb, ctx->cid_table->dc_bits[nbits] + nbits,
              (ctx->cid_table->dc_codes[nbits] << nbits) +
-             (diff & ((1 << nbits) - 1)));
+             av_mod_uintp2(diff, nbits));
 }
 
 static av_always_inline
diff --git a/libavcodec/dvenc.c b/libavcodec/dvenc.c
index 2442f2b..9ce7273 100644
--- a/libavcodec/dvenc.c
+++ b/libavcodec/dvenc.c
@@ -176,7 +176,7 @@ static av_always_inline PutBitContext *dv_encode_ac(EncBlockInfo *bi,
             if (bits_left) {
                 size -= bits_left;
                 put_bits(pb, bits_left, vlc >> size);
-                vlc = vlc & ((1 << size) - 1);
+                vlc = av_mod_uintp2(vlc, size);
             }
             if (pb + 1 >= pb_end) {
                 bi->partial_bit_count  = size;
diff --git a/libavcodec/ffv1.h b/libavcodec/ffv1.h
index 5081397..bfc4d71 100644
--- a/libavcodec/ffv1.h
+++ b/libavcodec/ffv1.h
@@ -143,7 +143,7 @@ static av_always_inline int fold(int diff, int bits)
         diff = (int8_t)diff;
     else {
         diff +=  1 << (bits  - 1);
-        diff &= (1 <<  bits) - 1;
+        diff  = av_mod_uintp2(diff, bits);
         diff -=  1 << (bits  - 1);
     }
 
diff --git a/libavcodec/ffv1dec.c b/libavcodec/ffv1dec.c
index 78ba1be..fda3f09 100644
--- a/libavcodec/ffv1dec.c
+++ b/libavcodec/ffv1dec.c
@@ -172,8 +172,7 @@ static av_always_inline void decode_line(FFV1Context *s, int w,
         if (sign)
             diff = -diff;
 
-        sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &
-                       ((1 << bits) - 1);
+        sample[1][x] = av_mod_uintp2(predict(sample[1] + x, sample[0] + x) + diff, bits);
     }
     s->run_index = run_index;
 }
diff --git a/libavcodec/g726.c b/libavcodec/g726.c
index 5b9986a..5bbf897 100644
--- a/libavcodec/g726.c
+++ b/libavcodec/g726.c
@@ -298,7 +298,7 @@ static int16_t g726_encode(G726Context* c, int16_t sig)
 {
     uint8_t i;
 
-    i = quant(c, sig/4 - c->se) & ((1<<c->code_size) - 1);
+    i = av_mod_uintp2(quant(c, sig/4 - c->se), c->code_size);
     g726_decode(c, i);
     return i;
 }
diff --git a/libavcodec/g729dec.c b/libavcodec/g729dec.c
index e97677b..ed717ba 100644
--- a/libavcodec/g729dec.c
+++ b/libavcodec/g729dec.c
@@ -512,7 +512,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
 
         if (frame_erasure) {
             ctx->rand_value = g729_prng(ctx->rand_value);
-            fc_indexes   = ctx->rand_value & ((1 << format->fc_indexes_bits) - 1);
+            fc_indexes   = av_mod_uintp2(ctx->rand_value, format->fc_indexes_bits);
 
             ctx->rand_value = g729_prng(ctx->rand_value);
             pulses_signs = ctx->rand_value;
diff --git a/libavcodec/golomb.h b/libavcodec/golomb.h
index 28ae213..76da6a2 100644
--- a/libavcodec/golomb.h
+++ b/libavcodec/golomb.h
@@ -518,7 +518,7 @@ static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit,
 
     e = i >> k;
     if (e < limit)
-        put_bits(pb, e + k + 1, (1 << k) + (i & ((1 << k) - 1)));
+        put_bits(pb, e + k + 1, (1 << k) + av_mod_uintp2(i, k));
     else
         put_bits(pb, limit + esc_len, i - limit + 1);
 }
diff --git a/libavcodec/h264.c b/libavcodec/h264.c
index 0ed57a2..0f756a2 100644
--- a/libavcodec/h264.c
+++ b/libavcodec/h264.c
@@ -1559,9 +1559,8 @@ again:
                         h->valid_recovery_point = 1;
 
                     if (   h->recovery_frame < 0
-                        || ((h->recovery_frame - h->frame_num) & ((1 << h->sps.log2_max_frame_num)-1)) > h->sei_recovery_frame_cnt) {
-                        h->recovery_frame = (h->frame_num + h->sei_recovery_frame_cnt) &
-                                            ((1 << h->sps.log2_max_frame_num) - 1);
+                        || av_mod_uintp2(h->recovery_frame - h->frame_num, h->sps.log2_max_frame_num) > h->sei_recovery_frame_cnt) {
+                        h->recovery_frame = av_mod_uintp2(h->frame_num + h->sei_recovery_frame_cnt, h->sps.log2_max_frame_num);
 
                         if (!h->valid_recovery_point)
                             h->recovery_frame = h->frame_num;
diff --git a/libavcodec/h264_refs.c b/libavcodec/h264_refs.c
index c424b9e..0b2008a 100644
--- a/libavcodec/h264_refs.c
+++ b/libavcodec/h264_refs.c
@@ -772,7 +772,7 @@ int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count)
     for (i = 0; i<h->short_ref_count; i++) {
         pic = h->short_ref[i];
         if (pic->invalid_gap) {
-            int d = (h->cur_pic_ptr->frame_num - pic->frame_num) & ((1 << h->sps.log2_max_frame_num)-1);
+            int d = av_mod_uintp2(h->cur_pic_ptr->frame_num - pic->frame_num, h->sps.log2_max_frame_num);
             if (d > h->sps.ref_frame_count)
                 remove_short(h, pic->frame_num, 0);
         }
diff --git a/libavcodec/hevc.c b/libavcodec/hevc.c
index 926a19e..544854f 100644
--- a/libavcodec/hevc.c
+++ b/libavcodec/hevc.c
@@ -1461,8 +1461,8 @@ static void chroma_mc_uni(HEVCContext *s, uint8_t *dst0,
     int idx              = ff_hevc_pel_weight[block_w];
     int hshift           = s->sps->hshift[1];
     int vshift           = s->sps->vshift[1];
-    intptr_t mx          = mv->x & ((1 << (2 + hshift)) - 1);
-    intptr_t my          = mv->y & ((1 << (2 + vshift)) - 1);
+    intptr_t mx          = av_mod_uintp2(mv->x, 2 + hshift);
+    intptr_t my          = av_mod_uintp2(mv->y, 2 + vshift);
     intptr_t _mx         = mx << (1 - hshift);
     intptr_t _my         = my << (1 - vshift);
 
@@ -1530,10 +1530,10 @@ static void chroma_mc_bi(HEVCContext *s, uint8_t *dst0, ptrdiff_t dststride, AVF
     int hshift = s->sps->hshift[1];
     int vshift = s->sps->vshift[1];
 
-    intptr_t mx0 = mv0->x & ((1 << (2 + hshift)) - 1);
-    intptr_t my0 = mv0->y & ((1 << (2 + vshift)) - 1);
-    intptr_t mx1 = mv1->x & ((1 << (2 + hshift)) - 1);
-    intptr_t my1 = mv1->y & ((1 << (2 + vshift)) - 1);
+    intptr_t mx0 = av_mod_uintp2(mv0->x, 2 + hshift);
+    intptr_t my0 = av_mod_uintp2(mv0->y, 2 + vshift);
+    intptr_t mx1 = av_mod_uintp2(mv1->x, 2 + hshift);
+    intptr_t my1 = av_mod_uintp2(mv1->y, 2 + vshift);
     intptr_t _mx0 = mx0 << (1 - hshift);
     intptr_t _my0 = my0 << (1 - vshift);
     intptr_t _mx1 = mx1 << (1 - hshift);
@@ -1791,8 +1791,8 @@ static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
     int y_pu             = y0 >> s->sps->log2_min_pu_size;
     int min_pu_width     = s->sps->min_pu_width;
     int size_in_pus      = pu_size >> s->sps->log2_min_pu_size;
-    int x0b              = x0 & ((1 << s->sps->log2_ctb_size) - 1);
-    int y0b              = y0 & ((1 << s->sps->log2_ctb_size) - 1);
+    int x0b              = av_mod_uintp2(x0, s->sps->log2_ctb_size);
+    int y0b              = av_mod_uintp2(y0, s->sps->log2_ctb_size);
 
     int cand_up   = (lc->ctb_up_flag || y0b) ?
                     s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
diff --git a/libavcodec/hevc_cabac.c b/libavcodec/hevc_cabac.c
index 3862df7..cb369eb 100644
--- a/libavcodec/hevc_cabac.c
+++ b/libavcodec/hevc_cabac.c
@@ -658,8 +658,8 @@ int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0, int x_cb, int y_cb)
 {
     int min_cb_width = s->sps->min_cb_width;
     int inc = 0;
-    int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
-    int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
+    int x0b = av_mod_uintp2(x0, s->sps->log2_ctb_size);
+    int y0b = av_mod_uintp2(y0, s->sps->log2_ctb_size);
 
     if (s->HEVClc->ctb_left_flag || x0b)
         inc = !!SAMPLE_CTB(s->skip_flag, x_cb - 1, y_cb);
@@ -723,8 +723,8 @@ int ff_hevc_pred_mode_decode(HEVCContext *s)
 int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth, int x0, int y0)
 {
     int inc = 0, depth_left = 0, depth_top = 0;
-    int x0b  = x0 & ((1 << s->sps->log2_ctb_size) - 1);
-    int y0b  = y0 & ((1 << s->sps->log2_ctb_size) - 1);
+    int x0b  = av_mod_uintp2(x0, s->sps->log2_ctb_size);
+    int y0b  = av_mod_uintp2(y0, s->sps->log2_ctb_size);
     int x_cb = x0 >> s->sps->log2_min_cb_size;
     int y_cb = y0 >> s->sps->log2_min_cb_size;
 
diff --git a/libavcodec/hevc_mvs.c b/libavcodec/hevc_mvs.c
index 7e34669..e504257 100644
--- a/libavcodec/hevc_mvs.c
+++ b/libavcodec/hevc_mvs.c
@@ -42,8 +42,8 @@ void ff_hevc_set_neighbour_available(HEVCContext *s, int x0, int y0,
                                      int nPbW, int nPbH)
 {
     HEVCLocalContext *lc = s->HEVClc;
-    int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
-    int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
+    int x0b = av_mod_uintp2(x0, s->sps->log2_ctb_size);
+    int y0b = av_mod_uintp2(y0, s->sps->log2_ctb_size);
 
     lc->na.cand_up       = (lc->ctb_up_flag   || y0b);
     lc->na.cand_left     = (lc->ctb_left_flag || x0b);
diff --git a/libavcodec/hevc_ps.c b/libavcodec/hevc_ps.c
index 6a1a8f6..3d4f3e7 100644
--- a/libavcodec/hevc_ps.c
+++ b/libavcodec/hevc_ps.c
@@ -1123,8 +1123,8 @@ int ff_hevc_decode_nal_sps(HEVCContext *s)
 
     sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
 
-    if (sps->width  & ((1 << sps->log2_min_cb_size) - 1) ||
-        sps->height & ((1 << sps->log2_min_cb_size) - 1)) {
+    if (av_mod_uintp2(sps->width, sps->log2_min_cb_size) ||
+        av_mod_uintp2(sps->height, sps->log2_min_cb_size)) {
         av_log(s->avctx, AV_LOG_ERROR, "Invalid coded frame dimensions.\n");
         goto err;
     }
diff --git a/libavcodec/hevcpred_template.c b/libavcodec/hevcpred_template.c
index 8124230..6b763b3 100644
--- a/libavcodec/hevcpred_template.c
+++ b/libavcodec/hevcpred_template.c
@@ -117,8 +117,8 @@ do {                                  \
     if (s->pps->constrained_intra_pred_flag == 1) {
         int size_in_luma_pu_v = PU(size_in_luma_v);
         int size_in_luma_pu_h = PU(size_in_luma_h);
-        int on_pu_edge_x    = !(x0 & ((1 << s->sps->log2_min_pu_size) - 1));
-        int on_pu_edge_y    = !(y0 & ((1 << s->sps->log2_min_pu_size) - 1));
+        int on_pu_edge_x    = !av_mod_uintp2(x0, s->sps->log2_min_pu_size);
+        int on_pu_edge_y    = !av_mod_uintp2(y0, s->sps->log2_min_pu_size);
         if (!size_in_luma_pu_h)
             size_in_luma_pu_h++;
         if (cand_bottom_left == 1 && on_pu_edge_x) {
diff --git a/libavcodec/mpeg12enc.c b/libavcodec/mpeg12enc.c
index 9795b7f..edcce35 100644
--- a/libavcodec/mpeg12enc.c
+++ b/libavcodec/mpeg12enc.c
@@ -620,12 +620,12 @@ static inline void encode_dc(MpegEncContext *s, int diff, int component)
             put_bits(&s->pb,
                      ff_mpeg12_vlc_dc_lum_bits[index] + index,
                      (ff_mpeg12_vlc_dc_lum_code[index] << index) +
-                     (diff & ((1 << index) - 1)));
+                     av_mod_uintp2(diff, index));
         else
             put_bits(&s->pb,
                      ff_mpeg12_vlc_dc_chroma_bits[index] + index,
                      (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
-                     (diff & ((1 << index) - 1)));
+                     av_mod_uintp2(diff, index));
     } else {
         if (component == 0)
             put_bits(&s->pb,
@@ -1041,12 +1041,12 @@ av_cold void ff_mpeg1_encode_init(MpegEncContext *s)
 
             bits = ff_mpeg12_vlc_dc_lum_bits[index] + index;
             code = (ff_mpeg12_vlc_dc_lum_code[index] << index) +
-                   (diff & ((1 << index) - 1));
+                    av_mod_uintp2(diff, index);
             mpeg1_lum_dc_uni[i + 255] = bits + (code << 8);
 
             bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index;
             code = (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
-                   (diff & ((1 << index) - 1));
+                    av_mod_uintp2(diff, index);
             mpeg1_chr_dc_uni[i + 255] = bits + (code << 8);
         }
 
diff --git a/libavcodec/opus.h b/libavcodec/opus.h
index 1faa7d3..92bb28a 100644
--- a/libavcodec/opus.h
+++ b/libavcodec/opus.h
@@ -279,7 +279,7 @@ static av_always_inline unsigned int opus_getrawbits(OpusRangeCoder *rc, unsigne
         rc->rb.bytes--;
     }
 
-    value = rc->rb.cacheval & ((1<<count)-1);
+    value = av_mod_uintp2(rc->rb.cacheval, count);
     rc->rb.cacheval    >>= count;
     rc->rb.cachelen     -= count;
     rc->total_read_bits += count;
diff --git a/libavcodec/opus_celt.c b/libavcodec/opus_celt.c
index e5d615e..42623d9 100644
--- a/libavcodec/opus_celt.c
+++ b/libavcodec/opus_celt.c
@@ -1454,7 +1454,7 @@ static unsigned int celt_decode_band(CeltContext *s, OpusRangeCoder *rc,
         if (itheta == 0) {
             imid = 32767;
             iside = 0;
-            fill &= (1 << blocks) - 1;
+            fill = av_mod_uintp2(fill, blocks);
             delta = -16384;
         } else if (itheta == 16384) {
             imid = 0;
@@ -1666,7 +1666,7 @@ static unsigned int celt_decode_band(CeltContext *s, OpusRangeCoder *rc,
             for (j = 0; j < N0; j++)
                 lowband_out[j] = n * X[j];
         }
-        cm &= (1 << blocks) - 1;
+        cm = av_mod_uintp2(cm, blocks);
     }
     return cm;
 }
diff --git a/libavcodec/proresenc_kostya.c b/libavcodec/proresenc_kostya.c
index 440cc8b..a2a8d73 100644
--- a/libavcodec/proresenc_kostya.c
+++ b/libavcodec/proresenc_kostya.c
@@ -440,12 +440,11 @@ static int encode_slice_plane(ProresContext *ctx, PutBitContext *pb,
 
 static void put_alpha_diff(PutBitContext *pb, int cur, int prev, int abits)
 {
-    const int mask  = (1 << abits) - 1;
     const int dbits = (abits == 8) ? 4 : 7;
     const int dsize = 1 << dbits - 1;
     int diff = cur - prev;
 
-    diff &= mask;
+    diff = av_mod_uintp2(diff, abits);
     if (diff >= (1 << abits) - dsize)
         diff -= 1 << abits;
     if (diff < -dsize || diff > dsize || !diff) {
@@ -689,12 +688,11 @@ static int estimate_slice_plane(ProresContext *ctx, int *error, int plane,
 
 static int est_alpha_diff(int cur, int prev, int abits)
 {
-    const int mask  = (1 << abits) - 1;
     const int dbits = (abits == 8) ? 4 : 7;
     const int dsize = 1 << dbits - 1;
     int diff = cur - prev;
 
-    diff &= mask;
+    diff = av_mod_uintp2(diff, abits);
     if (diff >= (1 << abits) - dsize)
         diff -= 1 << abits;
     if (diff < -dsize || diff > dsize || !diff)
diff --git a/libavcodec/put_bits.h b/libavcodec/put_bits.h
index 81be6b3..760b233 100644
--- a/libavcodec/put_bits.h
+++ b/libavcodec/put_bits.h
@@ -193,7 +193,7 @@ static inline void put_sbits(PutBitContext *pb, int n, int32_t value)
 {
     av_assert2(n >= 0 && n <= 31);
 
-    put_bits(pb, n, value & ((1 << n) - 1));
+    put_bits(pb, n, av_mod_uintp2(value, n));
 }
 
 /**



More information about the ffmpeg-cvslog mailing list