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.

327 lines
9.9KB

  1. /*
  2. * Copyright (c) 2003 Michael Niedermayer
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * ASUS V1/V2 decoder.
  23. */
  24. #include "libavutil/attributes.h"
  25. #include "libavutil/mem.h"
  26. #include "asv.h"
  27. #include "avcodec.h"
  28. #include "put_bits.h"
  29. #include "dsputil.h"
  30. #include "mathops.h"
  31. #include "mpeg12data.h"
  32. //#undef NDEBUG
  33. //#include <assert.h>
  34. #define VLC_BITS 6
  35. #define ASV2_LEVEL_VLC_BITS 10
  36. static VLC ccp_vlc;
  37. static VLC level_vlc;
  38. static VLC dc_ccp_vlc;
  39. static VLC ac_ccp_vlc;
  40. static VLC asv2_level_vlc;
  41. static av_cold void init_vlcs(ASV1Context *a){
  42. static int done = 0;
  43. if (!done) {
  44. done = 1;
  45. INIT_VLC_STATIC(&ccp_vlc, VLC_BITS, 17,
  46. &ff_asv_ccp_tab[0][1], 2, 1,
  47. &ff_asv_ccp_tab[0][0], 2, 1, 64);
  48. INIT_VLC_STATIC(&dc_ccp_vlc, VLC_BITS, 8,
  49. &ff_asv_dc_ccp_tab[0][1], 2, 1,
  50. &ff_asv_dc_ccp_tab[0][0], 2, 1, 64);
  51. INIT_VLC_STATIC(&ac_ccp_vlc, VLC_BITS, 16,
  52. &ff_asv_ac_ccp_tab[0][1], 2, 1,
  53. &ff_asv_ac_ccp_tab[0][0], 2, 1, 64);
  54. INIT_VLC_STATIC(&level_vlc, VLC_BITS, 7,
  55. &ff_asv_level_tab[0][1], 2, 1,
  56. &ff_asv_level_tab[0][0], 2, 1, 64);
  57. INIT_VLC_STATIC(&asv2_level_vlc, ASV2_LEVEL_VLC_BITS, 63,
  58. &ff_asv2_level_tab[0][1], 2, 1,
  59. &ff_asv2_level_tab[0][0], 2, 1, 1024);
  60. }
  61. }
  62. //FIXME write a reversed bitstream reader to avoid the double reverse
  63. static inline int asv2_get_bits(GetBitContext *gb, int n){
  64. return ff_reverse[ get_bits(gb, n) << (8-n) ];
  65. }
  66. static inline int asv1_get_level(GetBitContext *gb){
  67. int code= get_vlc2(gb, level_vlc.table, VLC_BITS, 1);
  68. if(code==3) return get_sbits(gb, 8);
  69. else return code - 3;
  70. }
  71. static inline int asv2_get_level(GetBitContext *gb){
  72. int code= get_vlc2(gb, asv2_level_vlc.table, ASV2_LEVEL_VLC_BITS, 1);
  73. if(code==31) return (int8_t)asv2_get_bits(gb, 8);
  74. else return code - 31;
  75. }
  76. static inline int asv1_decode_block(ASV1Context *a, DCTELEM block[64]){
  77. int i;
  78. block[0]= 8*get_bits(&a->gb, 8);
  79. for(i=0; i<11; i++){
  80. const int ccp= get_vlc2(&a->gb, ccp_vlc.table, VLC_BITS, 1);
  81. if(ccp){
  82. if(ccp == 16) break;
  83. if(ccp < 0 || i>=10){
  84. av_log(a->avctx, AV_LOG_ERROR, "coded coeff pattern damaged\n");
  85. return -1;
  86. }
  87. if(ccp&8) block[a->scantable.permutated[4*i+0]]= (asv1_get_level(&a->gb) * a->intra_matrix[4*i+0])>>4;
  88. if(ccp&4) block[a->scantable.permutated[4*i+1]]= (asv1_get_level(&a->gb) * a->intra_matrix[4*i+1])>>4;
  89. if(ccp&2) block[a->scantable.permutated[4*i+2]]= (asv1_get_level(&a->gb) * a->intra_matrix[4*i+2])>>4;
  90. if(ccp&1) block[a->scantable.permutated[4*i+3]]= (asv1_get_level(&a->gb) * a->intra_matrix[4*i+3])>>4;
  91. }
  92. }
  93. return 0;
  94. }
  95. static inline int asv2_decode_block(ASV1Context *a, DCTELEM block[64]){
  96. int i, count, ccp;
  97. count= asv2_get_bits(&a->gb, 4);
  98. block[0]= 8*asv2_get_bits(&a->gb, 8);
  99. ccp= get_vlc2(&a->gb, dc_ccp_vlc.table, VLC_BITS, 1);
  100. if(ccp){
  101. if(ccp&4) block[a->scantable.permutated[1]]= (asv2_get_level(&a->gb) * a->intra_matrix[1])>>4;
  102. if(ccp&2) block[a->scantable.permutated[2]]= (asv2_get_level(&a->gb) * a->intra_matrix[2])>>4;
  103. if(ccp&1) block[a->scantable.permutated[3]]= (asv2_get_level(&a->gb) * a->intra_matrix[3])>>4;
  104. }
  105. for(i=1; i<count+1; i++){
  106. const int ccp= get_vlc2(&a->gb, ac_ccp_vlc.table, VLC_BITS, 1);
  107. if(ccp){
  108. if(ccp&8) block[a->scantable.permutated[4*i+0]]= (asv2_get_level(&a->gb) * a->intra_matrix[4*i+0])>>4;
  109. if(ccp&4) block[a->scantable.permutated[4*i+1]]= (asv2_get_level(&a->gb) * a->intra_matrix[4*i+1])>>4;
  110. if(ccp&2) block[a->scantable.permutated[4*i+2]]= (asv2_get_level(&a->gb) * a->intra_matrix[4*i+2])>>4;
  111. if(ccp&1) block[a->scantable.permutated[4*i+3]]= (asv2_get_level(&a->gb) * a->intra_matrix[4*i+3])>>4;
  112. }
  113. }
  114. return 0;
  115. }
  116. static inline int decode_mb(ASV1Context *a, DCTELEM block[6][64]){
  117. int i;
  118. a->dsp.clear_blocks(block[0]);
  119. if(a->avctx->codec_id == AV_CODEC_ID_ASV1){
  120. for(i=0; i<6; i++){
  121. if( asv1_decode_block(a, block[i]) < 0)
  122. return -1;
  123. }
  124. }else{
  125. for(i=0; i<6; i++){
  126. if( asv2_decode_block(a, block[i]) < 0)
  127. return -1;
  128. }
  129. }
  130. return 0;
  131. }
  132. static inline void idct_put(ASV1Context *a, int mb_x, int mb_y){
  133. DCTELEM (*block)[64]= a->block;
  134. int linesize= a->picture.linesize[0];
  135. uint8_t *dest_y = a->picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
  136. uint8_t *dest_cb = a->picture.data[1] + (mb_y * 8 * a->picture.linesize[1]) + mb_x * 8;
  137. uint8_t *dest_cr = a->picture.data[2] + (mb_y * 8 * a->picture.linesize[2]) + mb_x * 8;
  138. a->dsp.idct_put(dest_y , linesize, block[0]);
  139. a->dsp.idct_put(dest_y + 8, linesize, block[1]);
  140. a->dsp.idct_put(dest_y + 8*linesize , linesize, block[2]);
  141. a->dsp.idct_put(dest_y + 8*linesize + 8, linesize, block[3]);
  142. if(!(a->avctx->flags&CODEC_FLAG_GRAY)){
  143. a->dsp.idct_put(dest_cb, a->picture.linesize[1], block[4]);
  144. a->dsp.idct_put(dest_cr, a->picture.linesize[2], block[5]);
  145. }
  146. }
  147. static int decode_frame(AVCodecContext *avctx,
  148. void *data, int *data_size,
  149. AVPacket *avpkt)
  150. {
  151. const uint8_t *buf = avpkt->data;
  152. int buf_size = avpkt->size;
  153. ASV1Context * const a = avctx->priv_data;
  154. AVFrame *picture = data;
  155. AVFrame * const p= &a->picture;
  156. int mb_x, mb_y;
  157. if(p->data[0])
  158. avctx->release_buffer(avctx, p);
  159. p->reference= 0;
  160. if(avctx->get_buffer(avctx, p) < 0){
  161. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  162. return -1;
  163. }
  164. p->pict_type= AV_PICTURE_TYPE_I;
  165. p->key_frame= 1;
  166. av_fast_padded_malloc(&a->bitstream_buffer, &a->bitstream_buffer_size,
  167. buf_size);
  168. if (!a->bitstream_buffer)
  169. return AVERROR(ENOMEM);
  170. if(avctx->codec_id == AV_CODEC_ID_ASV1)
  171. a->dsp.bswap_buf((uint32_t*)a->bitstream_buffer, (const uint32_t*)buf, buf_size/4);
  172. else{
  173. int i;
  174. for(i=0; i<buf_size; i++)
  175. a->bitstream_buffer[i]= ff_reverse[ buf[i] ];
  176. }
  177. init_get_bits(&a->gb, a->bitstream_buffer, buf_size*8);
  178. for(mb_y=0; mb_y<a->mb_height2; mb_y++){
  179. for(mb_x=0; mb_x<a->mb_width2; mb_x++){
  180. if( decode_mb(a, a->block) <0)
  181. return -1;
  182. idct_put(a, mb_x, mb_y);
  183. }
  184. }
  185. if(a->mb_width2 != a->mb_width){
  186. mb_x= a->mb_width2;
  187. for(mb_y=0; mb_y<a->mb_height2; mb_y++){
  188. if( decode_mb(a, a->block) <0)
  189. return -1;
  190. idct_put(a, mb_x, mb_y);
  191. }
  192. }
  193. if(a->mb_height2 != a->mb_height){
  194. mb_y= a->mb_height2;
  195. for(mb_x=0; mb_x<a->mb_width; mb_x++){
  196. if( decode_mb(a, a->block) <0)
  197. return -1;
  198. idct_put(a, mb_x, mb_y);
  199. }
  200. }
  201. *picture = a->picture;
  202. *data_size = sizeof(AVPicture);
  203. emms_c();
  204. return (get_bits_count(&a->gb)+31)/32*4;
  205. }
  206. static av_cold int decode_init(AVCodecContext *avctx){
  207. ASV1Context * const a = avctx->priv_data;
  208. AVFrame *p= &a->picture;
  209. int i;
  210. const int scale= avctx->codec_id == AV_CODEC_ID_ASV1 ? 1 : 2;
  211. ff_asv_common_init(avctx);
  212. init_vlcs(a);
  213. ff_init_scantable(a->dsp.idct_permutation, &a->scantable, ff_asv_scantab);
  214. avctx->pix_fmt= AV_PIX_FMT_YUV420P;
  215. a->inv_qscale= avctx->extradata[0];
  216. if(a->inv_qscale == 0){
  217. av_log(avctx, AV_LOG_ERROR, "illegal qscale 0\n");
  218. if(avctx->codec_id == AV_CODEC_ID_ASV1)
  219. a->inv_qscale= 6;
  220. else
  221. a->inv_qscale= 10;
  222. }
  223. for(i=0; i<64; i++){
  224. int index = ff_asv_scantab[i];
  225. a->intra_matrix[i]= 64*scale*ff_mpeg1_default_intra_matrix[index] / a->inv_qscale;
  226. }
  227. p->qstride= a->mb_width;
  228. p->qscale_table= av_malloc( p->qstride * a->mb_height);
  229. p->quality= (32*scale + a->inv_qscale/2)/a->inv_qscale;
  230. memset(p->qscale_table, p->quality, p->qstride*a->mb_height);
  231. return 0;
  232. }
  233. static av_cold int decode_end(AVCodecContext *avctx){
  234. ASV1Context * const a = avctx->priv_data;
  235. av_freep(&a->bitstream_buffer);
  236. av_freep(&a->picture.qscale_table);
  237. a->bitstream_buffer_size=0;
  238. if(a->picture.data[0])
  239. avctx->release_buffer(avctx, &a->picture);
  240. return 0;
  241. }
  242. AVCodec ff_asv1_decoder = {
  243. .name = "asv1",
  244. .type = AVMEDIA_TYPE_VIDEO,
  245. .id = AV_CODEC_ID_ASV1,
  246. .priv_data_size = sizeof(ASV1Context),
  247. .init = decode_init,
  248. .close = decode_end,
  249. .decode = decode_frame,
  250. .capabilities = CODEC_CAP_DR1,
  251. .long_name = NULL_IF_CONFIG_SMALL("ASUS V1"),
  252. };
  253. AVCodec ff_asv2_decoder = {
  254. .name = "asv2",
  255. .type = AVMEDIA_TYPE_VIDEO,
  256. .id = AV_CODEC_ID_ASV2,
  257. .priv_data_size = sizeof(ASV1Context),
  258. .init = decode_init,
  259. .close = decode_end,
  260. .decode = decode_frame,
  261. .capabilities = CODEC_CAP_DR1,
  262. .long_name = NULL_IF_CONFIG_SMALL("ASUS V2"),
  263. };