Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * exp golomb vlc stuff
  3.  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4.  * Copyright (c) 2004 Alex Beregszaszi
  5.  *
  6.  * This file is part of FFmpeg.
  7.  *
  8.  * FFmpeg is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Lesser General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2.1 of the License, or (at your option) any later version.
  12.  *
  13.  * FFmpeg is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Lesser General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Lesser General Public
  19.  * License along with FFmpeg; if not, write to the Free Software
  20.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21.  */
  22.  
  23. /**
  24.  * @file
  25.  * @brief
  26.  *     exp golomb vlc stuff
  27.  * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
  28.  */
  29.  
  30. #ifndef AVCODEC_GOLOMB_H
  31. #define AVCODEC_GOLOMB_H
  32.  
  33. #include <stdint.h>
  34.  
  35. #include "get_bits.h"
  36. #include "put_bits.h"
  37.  
  38. #define INVALID_VLC           0x80000000
  39.  
  40. extern const uint8_t ff_golomb_vlc_len[512];
  41. extern const uint8_t ff_ue_golomb_vlc_code[512];
  42. extern const  int8_t ff_se_golomb_vlc_code[512];
  43. extern const uint8_t ff_ue_golomb_len[256];
  44.  
  45. extern const uint8_t ff_interleaved_golomb_vlc_len[256];
  46. extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
  47. extern const  int8_t ff_interleaved_se_golomb_vlc_code[256];
  48. extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
  49.  
  50. /**
  51.  * read unsigned exp golomb code.
  52.  */
  53. static inline int get_ue_golomb(GetBitContext *gb)
  54. {
  55.     unsigned int buf;
  56.  
  57.     OPEN_READER(re, gb);
  58.     UPDATE_CACHE(re, gb);
  59.     buf = GET_CACHE(re, gb);
  60.  
  61.     if (buf >= (1 << 27)) {
  62.         buf >>= 32 - 9;
  63.         LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  64.         CLOSE_READER(re, gb);
  65.  
  66.         return ff_ue_golomb_vlc_code[buf];
  67.     } else {
  68.         int log = 2 * av_log2(buf) - 31;
  69.         LAST_SKIP_BITS(re, gb, 32 - log);
  70.         CLOSE_READER(re, gb);
  71.         if (log < 7) {
  72.             av_log(NULL, AV_LOG_ERROR, "Invalid UE golomb code\n");
  73.             return AVERROR_INVALIDDATA;
  74.         }
  75.         buf >>= log;
  76.         buf--;
  77.  
  78.         return buf;
  79.     }
  80. }
  81.  
  82. /**
  83.  * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
  84.  */
  85. static inline unsigned get_ue_golomb_long(GetBitContext *gb)
  86. {
  87.     unsigned buf, log;
  88.  
  89.     buf = show_bits_long(gb, 32);
  90.     log = 31 - av_log2(buf);
  91.     skip_bits_long(gb, log);
  92.  
  93.     return get_bits_long(gb, log + 1) - 1;
  94. }
  95.  
  96. /**
  97.  * read unsigned exp golomb code, constraint to a max of 31.
  98.  * the return value is undefined if the stored value exceeds 31.
  99.  */
  100. static inline int get_ue_golomb_31(GetBitContext *gb)
  101. {
  102.     unsigned int buf;
  103.  
  104.     OPEN_READER(re, gb);
  105.     UPDATE_CACHE(re, gb);
  106.     buf = GET_CACHE(re, gb);
  107.  
  108.     buf >>= 32 - 9;
  109.     LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  110.     CLOSE_READER(re, gb);
  111.  
  112.     return ff_ue_golomb_vlc_code[buf];
  113. }
  114.  
  115. static inline unsigned svq3_get_ue_golomb(GetBitContext *gb)
  116. {
  117.     uint32_t buf;
  118.  
  119.     OPEN_READER(re, gb);
  120.     UPDATE_CACHE(re, gb);
  121.     buf = GET_CACHE(re, gb);
  122.  
  123.     if (buf & 0xAA800000) {
  124.         buf >>= 32 - 8;
  125.         LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
  126.         CLOSE_READER(re, gb);
  127.  
  128.         return ff_interleaved_ue_golomb_vlc_code[buf];
  129.     } else {
  130.         unsigned ret = 1;
  131.  
  132.         do {
  133.             buf >>= 32 - 8;
  134.             LAST_SKIP_BITS(re, gb,
  135.                            FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
  136.  
  137.             if (ff_interleaved_golomb_vlc_len[buf] != 9) {
  138.                 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
  139.                 ret  |= ff_interleaved_dirac_golomb_vlc_code[buf];
  140.                 break;
  141.             }
  142.             ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
  143.             UPDATE_CACHE(re, gb);
  144.             buf = GET_CACHE(re, gb);
  145.         } while (ret<0x8000000U && BITS_AVAILABLE(re, gb));
  146.  
  147.         CLOSE_READER(re, gb);
  148.         return ret - 1;
  149.     }
  150. }
  151.  
  152. /**
  153.  * read unsigned truncated exp golomb code.
  154.  */
  155. static inline int get_te0_golomb(GetBitContext *gb, int range)
  156. {
  157.     av_assert2(range >= 1);
  158.  
  159.     if (range == 1)
  160.         return 0;
  161.     else if (range == 2)
  162.         return get_bits1(gb) ^ 1;
  163.     else
  164.         return get_ue_golomb(gb);
  165. }
  166.  
  167. /**
  168.  * read unsigned truncated exp golomb code.
  169.  */
  170. static inline int get_te_golomb(GetBitContext *gb, int range)
  171. {
  172.     av_assert2(range >= 1);
  173.  
  174.     if (range == 2)
  175.         return get_bits1(gb) ^ 1;
  176.     else
  177.         return get_ue_golomb(gb);
  178. }
  179.  
  180. /**
  181.  * read signed exp golomb code.
  182.  */
  183. static inline int get_se_golomb(GetBitContext *gb)
  184. {
  185.     unsigned int buf;
  186.  
  187.     OPEN_READER(re, gb);
  188.     UPDATE_CACHE(re, gb);
  189.     buf = GET_CACHE(re, gb);
  190.  
  191.     if (buf >= (1 << 27)) {
  192.         buf >>= 32 - 9;
  193.         LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  194.         CLOSE_READER(re, gb);
  195.  
  196.         return ff_se_golomb_vlc_code[buf];
  197.     } else {
  198.         int log = av_log2(buf), sign;
  199.         LAST_SKIP_BITS(re, gb, 31 - log);
  200.         UPDATE_CACHE(re, gb);
  201.         buf = GET_CACHE(re, gb);
  202.  
  203.         buf >>= log;
  204.  
  205.         LAST_SKIP_BITS(re, gb, 32 - log);
  206.         CLOSE_READER(re, gb);
  207.  
  208.         sign = -(buf & 1);
  209.         buf  = ((buf >> 1) ^ sign) - sign;
  210.  
  211.         return buf;
  212.     }
  213. }
  214.  
  215. static inline int get_se_golomb_long(GetBitContext *gb)
  216. {
  217.     unsigned int buf = get_ue_golomb_long(gb);
  218.     int sign = (buf & 1) - 1;
  219.     return ((buf >> 1) ^ sign) + 1;
  220. }
  221.  
  222. static inline int svq3_get_se_golomb(GetBitContext *gb)
  223. {
  224.     unsigned int buf;
  225.  
  226.     OPEN_READER(re, gb);
  227.     UPDATE_CACHE(re, gb);
  228.     buf = GET_CACHE(re, gb);
  229.  
  230.     if (buf & 0xAA800000) {
  231.         buf >>= 32 - 8;
  232.         LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
  233.         CLOSE_READER(re, gb);
  234.  
  235.         return ff_interleaved_se_golomb_vlc_code[buf];
  236.     } else {
  237.         int log;
  238.         LAST_SKIP_BITS(re, gb, 8);
  239.         UPDATE_CACHE(re, gb);
  240.         buf |= 1 | (GET_CACHE(re, gb) >> 8);
  241.  
  242.         if ((buf & 0xAAAAAAAA) == 0)
  243.             return INVALID_VLC;
  244.  
  245.         for (log = 31; (buf & 0x80000000) == 0; log--)
  246.             buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
  247.  
  248.         LAST_SKIP_BITS(re, gb, 63 - 2 * log - 8);
  249.         CLOSE_READER(re, gb);
  250.  
  251.         return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
  252.     }
  253. }
  254.  
  255. static inline int dirac_get_se_golomb(GetBitContext *gb)
  256. {
  257.     uint32_t ret = svq3_get_ue_golomb(gb);
  258.  
  259.     if (ret) {
  260.         int sign = -get_bits1(gb);
  261.         ret = (ret ^ sign) - sign;
  262.     }
  263.  
  264.     return ret;
  265. }
  266.  
  267. /**
  268.  * read unsigned golomb rice code (ffv1).
  269.  */
  270. static inline int get_ur_golomb(GetBitContext *gb, int k, int limit,
  271.                                 int esc_len)
  272. {
  273.     unsigned int buf;
  274.     int log;
  275.  
  276.     OPEN_READER(re, gb);
  277.     UPDATE_CACHE(re, gb);
  278.     buf = GET_CACHE(re, gb);
  279.  
  280.     log = av_log2(buf);
  281.  
  282.     if (log > 31 - limit) {
  283.         buf >>= log - k;
  284.         buf  += (30U - log) << k;
  285.         LAST_SKIP_BITS(re, gb, 32 + k - log);
  286.         CLOSE_READER(re, gb);
  287.  
  288.         return buf;
  289.     } else {
  290.         LAST_SKIP_BITS(re, gb, limit);
  291.         UPDATE_CACHE(re, gb);
  292.  
  293.         buf = SHOW_UBITS(re, gb, esc_len);
  294.  
  295.         LAST_SKIP_BITS(re, gb, esc_len);
  296.         CLOSE_READER(re, gb);
  297.  
  298.         return buf + limit - 1;
  299.     }
  300. }
  301.  
  302. /**
  303.  * read unsigned golomb rice code (jpegls).
  304.  */
  305. static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit,
  306.                                        int esc_len)
  307. {
  308.     unsigned int buf;
  309.     int log;
  310.  
  311.     OPEN_READER(re, gb);
  312.     UPDATE_CACHE(re, gb);
  313.     buf = GET_CACHE(re, gb);
  314.  
  315.     log = av_log2(buf);
  316.  
  317.     if (log - k >= 32 - MIN_CACHE_BITS + (MIN_CACHE_BITS == 32) &&
  318.         32 - log < limit) {
  319.         buf >>= log - k;
  320.         buf  += (30U - log) << k;
  321.         LAST_SKIP_BITS(re, gb, 32 + k - log);
  322.         CLOSE_READER(re, gb);
  323.  
  324.         return buf;
  325.     } else {
  326.         int i;
  327.         for (i = 0; i < limit && SHOW_UBITS(re, gb, 1) == 0; i++) {
  328.             if (gb->size_in_bits <= re_index)
  329.                 return -1;
  330.             LAST_SKIP_BITS(re, gb, 1);
  331.             UPDATE_CACHE(re, gb);
  332.         }
  333.         SKIP_BITS(re, gb, 1);
  334.  
  335.         if (i < limit - 1) {
  336.             if (k) {
  337.                 if (k > MIN_CACHE_BITS - 1) {
  338.                     buf = SHOW_UBITS(re, gb, 16) << (k-16);
  339.                     LAST_SKIP_BITS(re, gb, 16);
  340.                     UPDATE_CACHE(re, gb);
  341.                     buf |= SHOW_UBITS(re, gb, k-16);
  342.                     LAST_SKIP_BITS(re, gb, k-16);
  343.                 } else {
  344.                     buf = SHOW_UBITS(re, gb, k);
  345.                     LAST_SKIP_BITS(re, gb, k);
  346.                 }
  347.             } else {
  348.                 buf = 0;
  349.             }
  350.  
  351.             CLOSE_READER(re, gb);
  352.             return buf + (i << k);
  353.         } else if (i == limit - 1) {
  354.             buf = SHOW_UBITS(re, gb, esc_len);
  355.             LAST_SKIP_BITS(re, gb, esc_len);
  356.             CLOSE_READER(re, gb);
  357.  
  358.             return buf + 1;
  359.         } else
  360.             return -1;
  361.     }
  362. }
  363.  
  364. /**
  365.  * read signed golomb rice code (ffv1).
  366.  */
  367. static inline int get_sr_golomb(GetBitContext *gb, int k, int limit,
  368.                                 int esc_len)
  369. {
  370.     unsigned v = get_ur_golomb(gb, k, limit, esc_len);
  371.     return (v >> 1) ^ -(v & 1);
  372. }
  373.  
  374. /**
  375.  * read signed golomb rice code (flac).
  376.  */
  377. static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit,
  378.                                      int esc_len)
  379. {
  380.     unsigned v = get_ur_golomb_jpegls(gb, k, limit, esc_len);
  381.     return (v >> 1) ^ -(v & 1);
  382. }
  383.  
  384. /**
  385.  * read unsigned golomb rice code (shorten).
  386.  */
  387. static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
  388. {
  389.     return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
  390. }
  391.  
  392. /**
  393.  * read signed golomb rice code (shorten).
  394.  */
  395. static inline int get_sr_golomb_shorten(GetBitContext *gb, int k)
  396. {
  397.     int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
  398.     return (uvar >> 1) ^ -(uvar & 1);
  399. }
  400.  
  401. #ifdef TRACE
  402.  
  403. static inline int get_ue(GetBitContext *s, const char *file, const char *func,
  404.                          int line)
  405. {
  406.     int show = show_bits(s, 24);
  407.     int pos  = get_bits_count(s);
  408.     int i    = get_ue_golomb(s);
  409.     int len  = get_bits_count(s) - pos;
  410.     int bits = show >> (24 - len);
  411.  
  412.     print_bin(bits, len);
  413.  
  414.     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue  @%5d in %s %s:%d\n",
  415.            bits, len, i, pos, file, func, line);
  416.  
  417.     return i;
  418. }
  419.  
  420. static inline int get_se(GetBitContext *s, const char *file, const char *func,
  421.                          int line)
  422. {
  423.     int show = show_bits(s, 24);
  424.     int pos  = get_bits_count(s);
  425.     int i    = get_se_golomb(s);
  426.     int len  = get_bits_count(s) - pos;
  427.     int bits = show >> (24 - len);
  428.  
  429.     print_bin(bits, len);
  430.  
  431.     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se  @%5d in %s %s:%d\n",
  432.            bits, len, i, pos, file, func, line);
  433.  
  434.     return i;
  435. }
  436.  
  437. static inline int get_te(GetBitContext *s, int r, char *file, const char *func,
  438.                          int line)
  439. {
  440.     int show = show_bits(s, 24);
  441.     int pos  = get_bits_count(s);
  442.     int i    = get_te0_golomb(s, r);
  443.     int len  = get_bits_count(s) - pos;
  444.     int bits = show >> (24 - len);
  445.  
  446.     print_bin(bits, len);
  447.  
  448.     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te  @%5d in %s %s:%d\n",
  449.            bits, len, i, pos, file, func, line);
  450.  
  451.     return i;
  452. }
  453.  
  454. #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  455. #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  456. #define get_te_golomb(a, r)  get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  457. #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  458.  
  459. #endif /* TRACE */
  460.  
  461. /**
  462.  * write unsigned exp golomb code.
  463.  */
  464. static inline void set_ue_golomb(PutBitContext *pb, int i)
  465. {
  466.     av_assert2(i >= 0);
  467.  
  468.     if (i < 256)
  469.         put_bits(pb, ff_ue_golomb_len[i], i + 1);
  470.     else {
  471.         int e = av_log2(i + 1);
  472.         put_bits(pb, 2 * e + 1, i + 1);
  473.     }
  474. }
  475.  
  476. /**
  477.  * write truncated unsigned exp golomb code.
  478.  */
  479. static inline void set_te_golomb(PutBitContext *pb, int i, int range)
  480. {
  481.     av_assert2(range >= 1);
  482.     av_assert2(i <= range);
  483.  
  484.     if (range == 2)
  485.         put_bits(pb, 1, i ^ 1);
  486.     else
  487.         set_ue_golomb(pb, i);
  488. }
  489.  
  490. /**
  491.  * write signed exp golomb code. 16 bits at most.
  492.  */
  493. static inline void set_se_golomb(PutBitContext *pb, int i)
  494. {
  495. #if 0
  496.     if (i <= 0)
  497.         i = -2 * i;
  498.     else
  499.         i = 2 * i - 1;
  500. #elif 1
  501.     i = 2 * i - 1;
  502.     if (i < 0)
  503.         i ^= -1;    //FIXME check if gcc does the right thing
  504. #else
  505.     i  = 2 * i - 1;
  506.     i ^= (i >> 31);
  507. #endif
  508.     set_ue_golomb(pb, i);
  509. }
  510.  
  511. /**
  512.  * write unsigned golomb rice code (ffv1).
  513.  */
  514. static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit,
  515.                                  int esc_len)
  516. {
  517.     int e;
  518.  
  519.     av_assert2(i >= 0);
  520.  
  521.     e = i >> k;
  522.     if (e < limit)
  523.         put_bits(pb, e + k + 1, (1 << k) + av_mod_uintp2(i, k));
  524.     else
  525.         put_bits(pb, limit + esc_len, i - limit + 1);
  526. }
  527.  
  528. /**
  529.  * write unsigned golomb rice code (jpegls).
  530.  */
  531. static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k,
  532.                                         int limit, int esc_len)
  533. {
  534.     int e;
  535.  
  536.     av_assert2(i >= 0);
  537.  
  538.     e = (i >> k) + 1;
  539.     if (e < limit) {
  540.         while (e > 31) {
  541.             put_bits(pb, 31, 0);
  542.             e -= 31;
  543.         }
  544.         put_bits(pb, e, 1);
  545.         if (k)
  546.             put_sbits(pb, k, i);
  547.     } else {
  548.         while (limit > 31) {
  549.             put_bits(pb, 31, 0);
  550.             limit -= 31;
  551.         }
  552.         put_bits(pb, limit, 1);
  553.         put_bits(pb, esc_len, i - 1);
  554.     }
  555. }
  556.  
  557. /**
  558.  * write signed golomb rice code (ffv1).
  559.  */
  560. static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit,
  561.                                  int esc_len)
  562. {
  563.     int v;
  564.  
  565.     v  = -2 * i - 1;
  566.     v ^= (v >> 31);
  567.  
  568.     set_ur_golomb(pb, v, k, limit, esc_len);
  569. }
  570.  
  571. /**
  572.  * write signed golomb rice code (flac).
  573.  */
  574. static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k,
  575.                                       int limit, int esc_len)
  576. {
  577.     int v;
  578.  
  579.     v  = -2 * i - 1;
  580.     v ^= (v >> 31);
  581.  
  582.     set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
  583. }
  584.  
  585. #endif /* AVCODEC_GOLOMB_H */
  586.