You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
8.4KB

  1. /*
  2. * RV40 decoder
  3. * Copyright (c) 2007 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file rv40.c
  23. * RV40 decoder
  24. */
  25. #include "avcodec.h"
  26. #include "dsputil.h"
  27. #include "mpegvideo.h"
  28. #include "rv34.h"
  29. #include "rv40vlc2.h"
  30. #include "rv40data.h"
  31. static VLC aic_top_vlc;
  32. static VLC aic_mode1_vlc[AIC_MODE1_NUM], aic_mode2_vlc[AIC_MODE2_NUM];
  33. static VLC ptype_vlc[NUM_PTYPE_VLCS], btype_vlc[NUM_BTYPE_VLCS];
  34. /**
  35. * Initialize all tables.
  36. */
  37. static void rv40_init_tables()
  38. {
  39. int i;
  40. init_vlc(&aic_top_vlc, AIC_TOP_BITS, AIC_TOP_SIZE,
  41. rv40_aic_top_vlc_bits, 1, 1,
  42. rv40_aic_top_vlc_codes, 1, 1, INIT_VLC_USE_STATIC);
  43. for(i = 0; i < AIC_MODE1_NUM; i++){
  44. // Every tenth VLC table is empty
  45. if((i % 10) == 9) continue;
  46. init_vlc(&aic_mode1_vlc[i], AIC_MODE1_BITS, AIC_MODE1_SIZE,
  47. aic_mode1_vlc_bits[i], 1, 1,
  48. aic_mode1_vlc_codes[i], 1, 1, INIT_VLC_USE_STATIC);
  49. }
  50. for(i = 0; i < AIC_MODE2_NUM; i++){
  51. init_vlc(&aic_mode2_vlc[i], AIC_MODE2_BITS, AIC_MODE2_SIZE,
  52. aic_mode2_vlc_bits[i], 1, 1,
  53. aic_mode2_vlc_codes[i], 2, 2, INIT_VLC_USE_STATIC);
  54. }
  55. for(i = 0; i < NUM_PTYPE_VLCS; i++)
  56. init_vlc_sparse(&ptype_vlc[i], PTYPE_VLC_BITS, PTYPE_VLC_SIZE,
  57. ptype_vlc_bits[i], 1, 1,
  58. ptype_vlc_codes[i], 1, 1,
  59. ptype_vlc_syms, 1, 1, INIT_VLC_USE_STATIC);
  60. for(i = 0; i < NUM_BTYPE_VLCS; i++)
  61. init_vlc_sparse(&btype_vlc[i], BTYPE_VLC_BITS, BTYPE_VLC_SIZE,
  62. btype_vlc_bits[i], 1, 1,
  63. btype_vlc_codes[i], 1, 1,
  64. btype_vlc_syms, 1, 1, INIT_VLC_USE_STATIC);
  65. }
  66. /**
  67. * Get stored dimension from bitstream.
  68. *
  69. * If the width/height is the standard one then it's coded as a 3-bit index.
  70. * Otherwise it is coded as escaped 8-bit portions.
  71. */
  72. static int get_dimension(GetBitContext *gb, const int *dim)
  73. {
  74. int t = get_bits(gb, 3);
  75. int val = dim[t];
  76. if(val < 0)
  77. val = dim[get_bits1(gb) - val];
  78. if(!val){
  79. do{
  80. t = get_bits(gb, 8);
  81. val += t << 2;
  82. }while(t == 0xFF);
  83. }
  84. return val;
  85. }
  86. /**
  87. * Get encoded picture size - usually this is called from rv40_parse_slice_header.
  88. */
  89. static void rv40_parse_picture_size(GetBitContext *gb, int *w, int *h)
  90. {
  91. *w = get_dimension(gb, rv40_standard_widths);
  92. *h = get_dimension(gb, rv40_standard_heights);
  93. }
  94. static int rv40_parse_slice_header(RV34DecContext *r, GetBitContext *gb, SliceInfo *si)
  95. {
  96. int t, mb_bits;
  97. int w = r->s.width, h = r->s.height;
  98. int mb_size;
  99. memset(si, 0, sizeof(SliceInfo));
  100. if(get_bits1(gb))
  101. return -1;
  102. si->type = get_bits(gb, 2);
  103. if(si->type == 1) si->type = 0;
  104. si->quant = get_bits(gb, 5);
  105. if(get_bits(gb, 2))
  106. return -1;
  107. si->vlc_set = get_bits(gb, 2);
  108. skip_bits1(gb);
  109. t = get_bits(gb, 13); /// ???
  110. if(!si->type || !get_bits1(gb))
  111. rv40_parse_picture_size(gb, &w, &h);
  112. si->width = w;
  113. si->height = h;
  114. mb_size = ((w + 15) >> 4) * ((h + 15) >> 4);
  115. mb_bits = ff_rv34_get_start_offset(gb, mb_size);
  116. si->start = get_bits(gb, mb_bits);
  117. return 0;
  118. }
  119. /**
  120. * Decode 4x4 intra types array.
  121. */
  122. static int rv40_decode_intra_types(RV34DecContext *r, GetBitContext *gb, int *dst)
  123. {
  124. MpegEncContext *s = &r->s;
  125. int i, j, k, v;
  126. int A, B, C;
  127. int pattern;
  128. int *ptr;
  129. for(i = 0; i < 4; i++, dst += s->b4_stride){
  130. if(!i && s->first_slice_line){
  131. pattern = get_vlc2(gb, aic_top_vlc.table, AIC_TOP_BITS, 1);
  132. dst[0] = (pattern >> 2) & 2;
  133. dst[1] = (pattern >> 1) & 2;
  134. dst[2] = pattern & 2;
  135. dst[3] = (pattern << 1) & 2;
  136. continue;
  137. }
  138. ptr = dst;
  139. for(j = 0; j < 4; j++){
  140. /* Coefficients are read using VLC chosen by the prediction pattern
  141. * The first one (used for retrieving a pair of coefficients) is
  142. * constructed from the top, top right and left coefficients
  143. * The second one (used for retrieving only one coefficient) is
  144. * top + 10 * left.
  145. */
  146. A = ptr[-s->b4_stride + 1]; // it won't be used for the last coefficient in a row
  147. B = ptr[-s->b4_stride];
  148. C = ptr[-1];
  149. pattern = A + (B << 4) + (C << 8);
  150. for(k = 0; k < MODE2_PATTERNS_NUM; k++)
  151. if(pattern == rv40_aic_table_index[k])
  152. break;
  153. if(j < 3 && k < MODE2_PATTERNS_NUM){ //pattern is found, decoding 2 coefficients
  154. v = get_vlc2(gb, aic_mode2_vlc[k].table, AIC_MODE2_BITS, 2);
  155. *ptr++ = v/9;
  156. *ptr++ = v%9;
  157. j++;
  158. }else{
  159. if(B != -1 && C != -1)
  160. v = get_vlc2(gb, aic_mode1_vlc[B + C*10].table, AIC_MODE1_BITS, 1);
  161. else{ // tricky decoding
  162. v = 0;
  163. switch(C){
  164. case -1: // code 0 -> 1, 1 -> 0
  165. if(B < 2)
  166. v = get_bits1(gb) ^ 1;
  167. break;
  168. case 0:
  169. case 2: // code 0 -> 2, 1 -> 0
  170. v = (get_bits1(gb) ^ 1) << 1;
  171. break;
  172. }
  173. }
  174. *ptr++ = v;
  175. }
  176. }
  177. }
  178. return 0;
  179. }
  180. /**
  181. * Decode macroblock information.
  182. */
  183. static int rv40_decode_mb_info(RV34DecContext *r)
  184. {
  185. MpegEncContext *s = &r->s;
  186. GetBitContext *gb = &s->gb;
  187. int q, i;
  188. int prev_type = 0;
  189. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  190. int blocks[RV34_MB_TYPES] = {0};
  191. int count = 0;
  192. if(!r->s.mb_skip_run)
  193. r->s.mb_skip_run = ff_rv34_get_gamma(gb);
  194. if(--r->s.mb_skip_run)
  195. return RV34_MB_SKIP;
  196. if(r->avail[0])
  197. blocks[r->mb_type[mb_pos - 1]]++;
  198. if(r->avail[1]){
  199. blocks[r->mb_type[mb_pos - s->mb_stride]]++;
  200. if(r->avail[2])
  201. blocks[r->mb_type[mb_pos - s->mb_stride + 1]]++;
  202. if(r->avail[3])
  203. blocks[r->mb_type[mb_pos - s->mb_stride - 1]]++;
  204. }
  205. for(i = 0; i < RV34_MB_TYPES; i++){
  206. if(blocks[i] > count){
  207. count = blocks[i];
  208. prev_type = i;
  209. }
  210. }
  211. if(s->pict_type == P_TYPE){
  212. prev_type = block_num_to_ptype_vlc_num[prev_type];
  213. q = get_vlc2(gb, ptype_vlc[prev_type].table, PTYPE_VLC_BITS, 1);
  214. if(q < PBTYPE_ESCAPE)
  215. return q;
  216. q = get_vlc2(gb, ptype_vlc[prev_type].table, PTYPE_VLC_BITS, 1);
  217. av_log(s->avctx, AV_LOG_ERROR, "Dquant for P-frame\n");
  218. }else{
  219. prev_type = block_num_to_btype_vlc_num[prev_type];
  220. q = get_vlc2(gb, btype_vlc[prev_type].table, BTYPE_VLC_BITS, 1);
  221. if(q < PBTYPE_ESCAPE)
  222. return q;
  223. q = get_vlc2(gb, btype_vlc[prev_type].table, BTYPE_VLC_BITS, 1);
  224. av_log(s->avctx, AV_LOG_ERROR, "Dquant for B-frame\n");
  225. }
  226. return 0;
  227. }
  228. /**
  229. * Initialize decoder.
  230. */
  231. static int rv40_decode_init(AVCodecContext *avctx)
  232. {
  233. RV34DecContext *r = avctx->priv_data;
  234. r->rv30 = 0;
  235. ff_rv34_decode_init(avctx);
  236. if(!aic_top_vlc.bits)
  237. rv40_init_tables();
  238. r->parse_slice_header = rv40_parse_slice_header;
  239. r->decode_intra_types = rv40_decode_intra_types;
  240. r->decode_mb_info = rv40_decode_mb_info;
  241. r->luma_dc_quant_i = rv40_luma_dc_quant[0];
  242. r->luma_dc_quant_p = rv40_luma_dc_quant[1];
  243. return 0;
  244. }
  245. AVCodec rv40_decoder = {
  246. "rv40",
  247. CODEC_TYPE_VIDEO,
  248. CODEC_ID_RV40,
  249. sizeof(RV34DecContext),
  250. rv40_decode_init,
  251. NULL,
  252. ff_rv34_decode_end,
  253. ff_rv34_decode_frame,
  254. };