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.

483 lines
13KB

  1. /*
  2. * ASUS V1 codec
  3. * Copyright (c) 2003 Michael Niedermayer
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file asv1.c
  21. * ASUS V1 codec.
  22. */
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #include "mpegvideo.h"
  26. //#undef NDEBUG
  27. //#include <assert.h>
  28. #define VLC_BITS 5
  29. typedef struct ASV1Context{
  30. AVCodecContext *avctx;
  31. DSPContext dsp;
  32. AVFrame picture;
  33. PutBitContext pb;
  34. GetBitContext gb;
  35. ScanTable scantable;
  36. int inv_qscale;
  37. int mb_width;
  38. int mb_height;
  39. int mb_width2;
  40. int mb_height2;
  41. DCTELEM __align8 block[6][64];
  42. uint16_t __align8 intra_matrix[64];
  43. int __align8 q_intra_matrix[64];
  44. uint8_t *bitstream_buffer;
  45. int bitstream_buffer_size;
  46. } ASV1Context;
  47. static const uint8_t scantab[64]={
  48. 0x00,0x08,0x01,0x09,0x10,0x18,0x11,0x19,
  49. 0x02,0x0A,0x03,0x0B,0x12,0x1A,0x13,0x1B,
  50. 0x04,0x0C,0x05,0x0D,0x20,0x28,0x21,0x29,
  51. 0x06,0x0E,0x07,0x0F,0x14,0x1C,0x15,0x1D,
  52. 0x22,0x2A,0x23,0x2B,0x30,0x38,0x31,0x39,
  53. };
  54. static const uint8_t ccp_tab[17][2]={
  55. {0x2,2}, {0xE,5}, {0xD,5}, {0xC,5},
  56. {0xB,5}, {0xA,5}, {0x9,5}, {0x8,5},
  57. {0x7,5}, {0x6,5}, {0x5,5}, {0x4,5},
  58. {0x3,5}, {0x2,5}, {0x1,5}, {0x3,2},
  59. {0xF,5}, //EOB
  60. };
  61. static const uint8_t level_tab[7][2]={
  62. {3,4}, {3,3}, {3,2}, {0,3}, {2,2}, {2,3}, {2,4}
  63. };
  64. static VLC ccp_vlc;
  65. static VLC level_vlc;
  66. static void init_vlcs(ASV1Context *a){
  67. static int done = 0;
  68. if (!done) {
  69. done = 1;
  70. init_vlc(&ccp_vlc, VLC_BITS, 17,
  71. &ccp_tab[0][1], 2, 1,
  72. &ccp_tab[0][0], 2, 1);
  73. init_vlc(&level_vlc, VLC_BITS, 7,
  74. &level_tab[0][1], 2, 1,
  75. &level_tab[0][0], 2, 1);
  76. }
  77. }
  78. static inline int get_level(GetBitContext *gb){
  79. int code= get_vlc2(gb, level_vlc.table, VLC_BITS, 1);
  80. if(code==3) return get_sbits(gb, 8);
  81. else return code - 3;
  82. }
  83. #ifdef CONFIG_ENCODERS
  84. static inline void put_level(PutBitContext *pb, int level){
  85. unsigned int index= level + 3;
  86. if(index <= 6) put_bits(pb, level_tab[index][1], level_tab[index][0]);
  87. else{
  88. put_bits(pb, level_tab[3][1], level_tab[3][0]);
  89. put_bits(pb, 8, level&0xFF);
  90. }
  91. }
  92. #endif //CONFIG_ENCODERS
  93. static inline int decode_block(ASV1Context *a, DCTELEM block[64]){
  94. int i;
  95. block[0]= 8*get_bits(&a->gb, 8);
  96. for(i=0; i<11; i++){
  97. const int ccp= get_vlc2(&a->gb, ccp_vlc.table, VLC_BITS, 1);
  98. if(ccp){
  99. if(ccp == 16) break;
  100. if(ccp < 0 || i>=10){
  101. printf("coded coeff pattern damaged\n");
  102. return -1;
  103. }
  104. if(ccp&1) block[a->scantable.permutated[4*i+0]]= (get_level(&a->gb) * a->intra_matrix[4*i+0])>>4;
  105. if(ccp&2) block[a->scantable.permutated[4*i+1]]= (get_level(&a->gb) * a->intra_matrix[4*i+1])>>4;;
  106. if(ccp&4) block[a->scantable.permutated[4*i+2]]= (get_level(&a->gb) * a->intra_matrix[4*i+2])>>4;;
  107. if(ccp&8) block[a->scantable.permutated[4*i+3]]= (get_level(&a->gb) * a->intra_matrix[4*i+3])>>4;;
  108. }
  109. }
  110. return 0;
  111. }
  112. #ifdef CONFIG_ENCODERS
  113. static inline void encode_block(ASV1Context *a, DCTELEM block[64]){
  114. int i;
  115. int nc_count=0;
  116. put_bits(&a->pb, 8, (block[0] + 32)>>6);
  117. block[0]= 0;
  118. for(i=0; i<10; i++){
  119. const int index= scantab[4*i];
  120. int ccp=0;
  121. if( (block[index + 0] = (block[index + 0]*a->q_intra_matrix[index + 0] + (1<<15))>>16) ) ccp |= 1;
  122. if( (block[index + 8] = (block[index + 8]*a->q_intra_matrix[index + 8] + (1<<15))>>16) ) ccp |= 2;
  123. if( (block[index + 1] = (block[index + 1]*a->q_intra_matrix[index + 1] + (1<<15))>>16) ) ccp |= 4;
  124. if( (block[index + 9] = (block[index + 9]*a->q_intra_matrix[index + 9] + (1<<15))>>16) ) ccp |= 8;
  125. if(ccp){
  126. for(;nc_count; nc_count--)
  127. put_bits(&a->pb, ccp_tab[0][1], ccp_tab[0][0]);
  128. put_bits(&a->pb, ccp_tab[ccp][1], ccp_tab[ccp][0]);
  129. if(ccp&1) put_level(&a->pb, block[index + 0]);
  130. if(ccp&2) put_level(&a->pb, block[index + 8]);
  131. if(ccp&4) put_level(&a->pb, block[index + 1]);
  132. if(ccp&8) put_level(&a->pb, block[index + 9]);
  133. }else{
  134. nc_count++;
  135. }
  136. }
  137. put_bits(&a->pb, ccp_tab[16][1], ccp_tab[16][0]);
  138. }
  139. #endif //CONFIG_ENCODERS
  140. static inline int decode_mb(ASV1Context *a, DCTELEM block[6][64]){
  141. int i;
  142. a->dsp.clear_blocks(block[0]);
  143. for(i=0; i<6; i++){
  144. if( decode_block(a, block[i]) < 0)
  145. return -1;
  146. }
  147. return 0;
  148. }
  149. #ifdef CONFIG_ENCODERS
  150. static inline void encode_mb(ASV1Context *a, DCTELEM block[6][64]){
  151. int i;
  152. for(i=0; i<6; i++){
  153. encode_block(a, block[i]);
  154. }
  155. }
  156. #endif //CONFIG_ENCODERS
  157. static inline void idct_put(ASV1Context *a, int mb_x, int mb_y){
  158. DCTELEM (*block)[64]= a->block;
  159. int linesize= a->picture.linesize[0];
  160. uint8_t *dest_y = a->picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
  161. uint8_t *dest_cb = a->picture.data[1] + (mb_y * 8 * a->picture.linesize[1]) + mb_x * 8;
  162. uint8_t *dest_cr = a->picture.data[2] + (mb_y * 8 * a->picture.linesize[2]) + mb_x * 8;
  163. a->dsp.idct_put(dest_y , linesize, block[0]);
  164. a->dsp.idct_put(dest_y + 8, linesize, block[1]);
  165. a->dsp.idct_put(dest_y + 8*linesize , linesize, block[2]);
  166. a->dsp.idct_put(dest_y + 8*linesize + 8, linesize, block[3]);
  167. if(!(a->avctx->flags&CODEC_FLAG_GRAY)){
  168. a->dsp.idct_put(dest_cb, a->picture.linesize[1], block[4]);
  169. a->dsp.idct_put(dest_cr, a->picture.linesize[2], block[5]);
  170. }
  171. }
  172. #ifdef CONFIG_ENCODERS
  173. static inline void dct_get(ASV1Context *a, int mb_x, int mb_y){
  174. DCTELEM (*block)[64]= a->block;
  175. int linesize= a->picture.linesize[0];
  176. int i;
  177. uint8_t *ptr_y = a->picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
  178. uint8_t *ptr_cb = a->picture.data[1] + (mb_y * 8 * a->picture.linesize[1]) + mb_x * 8;
  179. uint8_t *ptr_cr = a->picture.data[2] + (mb_y * 8 * a->picture.linesize[2]) + mb_x * 8;
  180. a->dsp.get_pixels(block[0], ptr_y , linesize);
  181. a->dsp.get_pixels(block[1], ptr_y + 8, linesize);
  182. a->dsp.get_pixels(block[2], ptr_y + 8*linesize , linesize);
  183. a->dsp.get_pixels(block[3], ptr_y + 8*linesize + 8, linesize);
  184. for(i=0; i<4; i++)
  185. a->dsp.fdct(block[i]);
  186. if(!(a->avctx->flags&CODEC_FLAG_GRAY)){
  187. a->dsp.get_pixels(block[4], ptr_cb, a->picture.linesize[1]);
  188. a->dsp.get_pixels(block[5], ptr_cr, a->picture.linesize[2]);
  189. for(i=4; i<6; i++)
  190. a->dsp.fdct(block[i]);
  191. }
  192. }
  193. #endif //CONFIG_ENCODERS
  194. static int decode_frame(AVCodecContext *avctx,
  195. void *data, int *data_size,
  196. uint8_t *buf, int buf_size)
  197. {
  198. ASV1Context * const a = avctx->priv_data;
  199. AVFrame *picture = data;
  200. AVFrame * const p= (AVFrame*)&a->picture;
  201. int mb_x, mb_y;
  202. *data_size = 0;
  203. /* special case for last picture */
  204. if (buf_size == 0) {
  205. return 0;
  206. }
  207. if(p->data[0])
  208. avctx->release_buffer(avctx, p);
  209. p->reference= 0;
  210. if(avctx->get_buffer(avctx, p) < 0){
  211. fprintf(stderr, "get_buffer() failed\n");
  212. return -1;
  213. }
  214. p->pict_type= I_TYPE;
  215. p->key_frame= 1;
  216. a->bitstream_buffer= av_fast_realloc(a->bitstream_buffer, &a->bitstream_buffer_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  217. a->dsp.bswap_buf((uint32_t*)a->bitstream_buffer, (uint32_t*)buf, buf_size/4);
  218. init_get_bits(&a->gb, a->bitstream_buffer, buf_size*8);
  219. for(mb_y=0; mb_y<a->mb_height2; mb_y++){
  220. for(mb_x=0; mb_x<a->mb_width2; mb_x++){
  221. if( decode_mb(a, a->block) <0)
  222. return -1;
  223. idct_put(a, mb_x, mb_y);
  224. }
  225. }
  226. if(a->mb_width2 != a->mb_width){
  227. mb_x= a->mb_width2;
  228. for(mb_y=0; mb_y<a->mb_height2; mb_y++){
  229. if( decode_mb(a, a->block) <0)
  230. return -1;
  231. idct_put(a, mb_x, mb_y);
  232. }
  233. }
  234. if(a->mb_height2 != a->mb_height){
  235. mb_y= a->mb_height2;
  236. for(mb_x=0; mb_x<a->mb_width; mb_x++){
  237. if( decode_mb(a, a->block) <0)
  238. return -1;
  239. idct_put(a, mb_x, mb_y);
  240. }
  241. }
  242. #if 0
  243. int i;
  244. printf("%d %d\n", 8*buf_size, get_bits_count(&a->gb));
  245. for(i=get_bits_count(&a->gb); i<8*buf_size; i++){
  246. printf("%d", get_bits1(&a->gb));
  247. }
  248. for(i=0; i<s->avctx->extradata_size; i++){
  249. printf("%c\n", ((uint8_t*)s->avctx->extradata)[i]);
  250. }
  251. #endif
  252. p->quality= (32 + a->inv_qscale/2)/a->inv_qscale;
  253. memset(p->qscale_table, p->quality, p->qstride*a->mb_height);
  254. *picture= *(AVFrame*)&a->picture;
  255. *data_size = sizeof(AVPicture);
  256. emms_c();
  257. return (get_bits_count(&a->gb)+31)/32*4;
  258. }
  259. #ifdef CONFIG_ENCODERS
  260. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  261. ASV1Context * const a = avctx->priv_data;
  262. AVFrame *pict = data;
  263. AVFrame * const p= (AVFrame*)&a->picture;
  264. int size;
  265. int mb_x, mb_y;
  266. init_put_bits(&a->pb, buf, buf_size, NULL, NULL);
  267. *p = *pict;
  268. p->pict_type= I_TYPE;
  269. p->key_frame= 1;
  270. for(mb_y=0; mb_y<a->mb_height2; mb_y++){
  271. for(mb_x=0; mb_x<a->mb_width2; mb_x++){
  272. dct_get(a, mb_x, mb_y);
  273. encode_mb(a, a->block);
  274. }
  275. }
  276. if(a->mb_width2 != a->mb_width){
  277. mb_x= a->mb_width2;
  278. for(mb_y=0; mb_y<a->mb_height2; mb_y++){
  279. dct_get(a, mb_x, mb_y);
  280. encode_mb(a, a->block);
  281. }
  282. }
  283. if(a->mb_height2 != a->mb_height){
  284. mb_y= a->mb_height2;
  285. for(mb_x=0; mb_x<a->mb_width; mb_x++){
  286. dct_get(a, mb_x, mb_y);
  287. encode_mb(a, a->block);
  288. }
  289. }
  290. emms_c();
  291. align_put_bits(&a->pb);
  292. while(get_bit_count(&a->pb)&31)
  293. put_bits(&a->pb, 8, 0);
  294. size= get_bit_count(&a->pb)/32;
  295. a->dsp.bswap_buf((uint32_t*)buf, (uint32_t*)buf, size);
  296. return size*4;
  297. }
  298. #endif //CONFIG_ENCODERS
  299. static void common_init(AVCodecContext *avctx){
  300. ASV1Context * const a = avctx->priv_data;
  301. dsputil_init(&a->dsp, avctx);
  302. a->mb_width = (avctx->width + 15) / 16;
  303. a->mb_height = (avctx->height + 15) / 16;
  304. a->mb_width2 = (avctx->width + 0) / 16;
  305. a->mb_height2 = (avctx->height + 0) / 16;
  306. avctx->coded_frame= (AVFrame*)&a->picture;
  307. a->avctx= avctx;
  308. }
  309. static int decode_init(AVCodecContext *avctx){
  310. ASV1Context * const a = avctx->priv_data;
  311. AVFrame *p= (AVFrame*)&a->picture;
  312. int i;
  313. common_init(avctx);
  314. init_vlcs(a);
  315. ff_init_scantable(a->dsp.idct_permutation, &a->scantable, scantab);
  316. a->inv_qscale= le2me_32(((uint32_t*)avctx->extradata)[0]);
  317. if(a->inv_qscale == 0){
  318. printf("illegal qscale 0\n");
  319. a->inv_qscale= 6;
  320. }
  321. for(i=0; i<64; i++){
  322. int index= scantab[i];
  323. a->intra_matrix[i]= 64*ff_mpeg1_default_intra_matrix[index] / a->inv_qscale;
  324. }
  325. p->qstride= a->mb_width;
  326. p->qscale_table= av_mallocz( p->qstride * a->mb_height);
  327. return 0;
  328. }
  329. #ifdef CONFIG_ENCODERS
  330. static int encode_init(AVCodecContext *avctx){
  331. ASV1Context * const a = avctx->priv_data;
  332. int i;
  333. common_init(avctx);
  334. if(avctx->global_quality == 0) avctx->global_quality= 4*FF_QUALITY_SCALE;
  335. a->inv_qscale= (32*FF_QUALITY_SCALE + avctx->global_quality/2) / avctx->global_quality;
  336. avctx->extradata= av_mallocz(8);
  337. avctx->extradata_size=8;
  338. ((uint32_t*)avctx->extradata)[0]= le2me_32(a->inv_qscale);
  339. ((uint32_t*)avctx->extradata)[1]= le2me_32(ff_get_fourcc("ASUS"));
  340. for(i=0; i<64; i++){
  341. int q= 32*ff_mpeg1_default_intra_matrix[i];
  342. a->q_intra_matrix[i]= ((a->inv_qscale<<16) + q/2) / q;
  343. }
  344. return 0;
  345. }
  346. #endif //CONFIG_ENCODERS
  347. static int decode_end(AVCodecContext *avctx){
  348. ASV1Context * const a = avctx->priv_data;
  349. av_freep(&a->bitstream_buffer);
  350. av_freep(&a->picture.qscale_table);
  351. a->bitstream_buffer_size=0;
  352. avcodec_default_free_buffers(avctx);
  353. return 0;
  354. }
  355. AVCodec asv1_decoder = {
  356. "asv1",
  357. CODEC_TYPE_VIDEO,
  358. CODEC_ID_ASV1,
  359. sizeof(ASV1Context),
  360. decode_init,
  361. NULL,
  362. decode_end,
  363. decode_frame,
  364. CODEC_CAP_DR1,
  365. };
  366. #ifdef CONFIG_ENCODERS
  367. AVCodec asv1_encoder = {
  368. "asv1",
  369. CODEC_TYPE_VIDEO,
  370. CODEC_ID_ASV1,
  371. sizeof(ASV1Context),
  372. encode_init,
  373. encode_frame,
  374. //encode_end,
  375. };
  376. #endif //CONFIG_ENCODERS