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.

459 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. static inline void put_level(PutBitContext *pb, int level){
  84. unsigned int index= level + 3;
  85. if(index <= 6) put_bits(pb, level_tab[index][1], level_tab[index][0]);
  86. else{
  87. put_bits(pb, level_tab[3][1], level_tab[3][0]);
  88. put_bits(pb, 8, level&0xFF);
  89. }
  90. }
  91. static inline int decode_block(ASV1Context *a, DCTELEM block[64]){
  92. int i;
  93. block[0]= 8*get_bits(&a->gb, 8);
  94. for(i=0; i<11; i++){
  95. const int ccp= get_vlc2(&a->gb, ccp_vlc.table, VLC_BITS, 1);
  96. if(ccp){
  97. if(ccp == 16) break;
  98. if(ccp < 0 || i>=10){
  99. printf("coded coeff pattern damaged\n");
  100. return -1;
  101. }
  102. if(ccp&1) block[a->scantable.permutated[4*i+0]]= (get_level(&a->gb) * a->intra_matrix[4*i+0])>>4;
  103. if(ccp&2) block[a->scantable.permutated[4*i+1]]= (get_level(&a->gb) * a->intra_matrix[4*i+1])>>4;
  104. if(ccp&4) block[a->scantable.permutated[4*i+2]]= (get_level(&a->gb) * a->intra_matrix[4*i+2])>>4;
  105. if(ccp&8) block[a->scantable.permutated[4*i+3]]= (get_level(&a->gb) * a->intra_matrix[4*i+3])>>4;
  106. }
  107. }
  108. return 0;
  109. }
  110. static inline void encode_block(ASV1Context *a, DCTELEM block[64]){
  111. int i;
  112. int nc_count=0;
  113. put_bits(&a->pb, 8, (block[0] + 32)>>6);
  114. block[0]= 0;
  115. for(i=0; i<10; i++){
  116. const int index= scantab[4*i];
  117. int ccp=0;
  118. if( (block[index + 0] = (block[index + 0]*a->q_intra_matrix[index + 0] + (1<<15))>>16) ) ccp |= 1;
  119. if( (block[index + 8] = (block[index + 8]*a->q_intra_matrix[index + 8] + (1<<15))>>16) ) ccp |= 2;
  120. if( (block[index + 1] = (block[index + 1]*a->q_intra_matrix[index + 1] + (1<<15))>>16) ) ccp |= 4;
  121. if( (block[index + 9] = (block[index + 9]*a->q_intra_matrix[index + 9] + (1<<15))>>16) ) ccp |= 8;
  122. if(ccp){
  123. for(;nc_count; nc_count--)
  124. put_bits(&a->pb, ccp_tab[0][1], ccp_tab[0][0]);
  125. put_bits(&a->pb, ccp_tab[ccp][1], ccp_tab[ccp][0]);
  126. if(ccp&1) put_level(&a->pb, block[index + 0]);
  127. if(ccp&2) put_level(&a->pb, block[index + 8]);
  128. if(ccp&4) put_level(&a->pb, block[index + 1]);
  129. if(ccp&8) put_level(&a->pb, block[index + 9]);
  130. }else{
  131. nc_count++;
  132. }
  133. }
  134. put_bits(&a->pb, ccp_tab[16][1], ccp_tab[16][0]);
  135. }
  136. static inline int decode_mb(ASV1Context *a, DCTELEM block[6][64]){
  137. int i;
  138. a->dsp.clear_blocks(block[0]);
  139. for(i=0; i<6; i++){
  140. if( decode_block(a, block[i]) < 0)
  141. return -1;
  142. }
  143. return 0;
  144. }
  145. static inline void encode_mb(ASV1Context *a, DCTELEM block[6][64]){
  146. int i;
  147. for(i=0; i<6; i++){
  148. encode_block(a, block[i]);
  149. }
  150. }
  151. static inline void idct_put(ASV1Context *a, int mb_x, int mb_y){
  152. DCTELEM (*block)[64]= a->block;
  153. int linesize= a->picture.linesize[0];
  154. uint8_t *dest_y = a->picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
  155. uint8_t *dest_cb = a->picture.data[1] + (mb_y * 8 * a->picture.linesize[1]) + mb_x * 8;
  156. uint8_t *dest_cr = a->picture.data[2] + (mb_y * 8 * a->picture.linesize[2]) + mb_x * 8;
  157. a->dsp.idct_put(dest_y , linesize, block[0]);
  158. a->dsp.idct_put(dest_y + 8, linesize, block[1]);
  159. a->dsp.idct_put(dest_y + 8*linesize , linesize, block[2]);
  160. a->dsp.idct_put(dest_y + 8*linesize + 8, linesize, block[3]);
  161. if(!(a->avctx->flags&CODEC_FLAG_GRAY)){
  162. a->dsp.idct_put(dest_cb, a->picture.linesize[1], block[4]);
  163. a->dsp.idct_put(dest_cr, a->picture.linesize[2], block[5]);
  164. }
  165. }
  166. static inline void dct_get(ASV1Context *a, int mb_x, int mb_y){
  167. DCTELEM (*block)[64]= a->block;
  168. int linesize= a->picture.linesize[0];
  169. int i;
  170. uint8_t *ptr_y = a->picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
  171. uint8_t *ptr_cb = a->picture.data[1] + (mb_y * 8 * a->picture.linesize[1]) + mb_x * 8;
  172. uint8_t *ptr_cr = a->picture.data[2] + (mb_y * 8 * a->picture.linesize[2]) + mb_x * 8;
  173. a->dsp.get_pixels(block[0], ptr_y , linesize);
  174. a->dsp.get_pixels(block[1], ptr_y + 8, linesize);
  175. a->dsp.get_pixels(block[2], ptr_y + 8*linesize , linesize);
  176. a->dsp.get_pixels(block[3], ptr_y + 8*linesize + 8, linesize);
  177. for(i=0; i<4; i++)
  178. a->dsp.fdct(block[i]);
  179. if(!(a->avctx->flags&CODEC_FLAG_GRAY)){
  180. a->dsp.get_pixels(block[4], ptr_cb, a->picture.linesize[1]);
  181. a->dsp.get_pixels(block[5], ptr_cr, a->picture.linesize[2]);
  182. for(i=4; i<6; i++)
  183. a->dsp.fdct(block[i]);
  184. }
  185. }
  186. static int decode_frame(AVCodecContext *avctx,
  187. void *data, int *data_size,
  188. uint8_t *buf, int buf_size)
  189. {
  190. ASV1Context * const a = avctx->priv_data;
  191. AVFrame *picture = data;
  192. AVFrame * const p= (AVFrame*)&a->picture;
  193. int mb_x, mb_y;
  194. *data_size = 0;
  195. /* special case for last picture */
  196. if (buf_size == 0) {
  197. return 0;
  198. }
  199. if(p->data[0])
  200. avctx->release_buffer(avctx, p);
  201. p->reference= 0;
  202. if(avctx->get_buffer(avctx, p) < 0){
  203. fprintf(stderr, "get_buffer() failed\n");
  204. return -1;
  205. }
  206. p->pict_type= I_TYPE;
  207. p->key_frame= 1;
  208. a->bitstream_buffer= av_fast_realloc(a->bitstream_buffer, &a->bitstream_buffer_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  209. a->dsp.bswap_buf((uint32_t*)a->bitstream_buffer, (uint32_t*)buf, buf_size/4);
  210. init_get_bits(&a->gb, a->bitstream_buffer, buf_size*8);
  211. for(mb_y=0; mb_y<a->mb_height2; mb_y++){
  212. for(mb_x=0; mb_x<a->mb_width2; mb_x++){
  213. if( decode_mb(a, a->block) <0)
  214. return -1;
  215. idct_put(a, mb_x, mb_y);
  216. }
  217. }
  218. if(a->mb_width2 != a->mb_width){
  219. mb_x= a->mb_width2;
  220. for(mb_y=0; mb_y<a->mb_height2; mb_y++){
  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_height2 != a->mb_height){
  227. mb_y= a->mb_height2;
  228. for(mb_x=0; mb_x<a->mb_width; mb_x++){
  229. if( decode_mb(a, a->block) <0)
  230. return -1;
  231. idct_put(a, mb_x, mb_y);
  232. }
  233. }
  234. #if 0
  235. int i;
  236. printf("%d %d\n", 8*buf_size, get_bits_count(&a->gb));
  237. for(i=get_bits_count(&a->gb); i<8*buf_size; i++){
  238. printf("%d", get_bits1(&a->gb));
  239. }
  240. for(i=0; i<s->avctx->extradata_size; i++){
  241. printf("%c\n", ((uint8_t*)s->avctx->extradata)[i]);
  242. }
  243. #endif
  244. p->quality= (32 + a->inv_qscale/2)/a->inv_qscale;
  245. memset(p->qscale_table, p->quality, p->qstride*a->mb_height);
  246. *picture= *(AVFrame*)&a->picture;
  247. *data_size = sizeof(AVPicture);
  248. emms_c();
  249. return (get_bits_count(&a->gb)+31)/32*4;
  250. }
  251. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  252. ASV1Context * const a = avctx->priv_data;
  253. AVFrame *pict = data;
  254. AVFrame * const p= (AVFrame*)&a->picture;
  255. int size;
  256. int mb_x, mb_y;
  257. init_put_bits(&a->pb, buf, buf_size, NULL, NULL);
  258. *p = *pict;
  259. p->pict_type= I_TYPE;
  260. p->key_frame= 1;
  261. for(mb_y=0; mb_y<a->mb_height2; mb_y++){
  262. for(mb_x=0; mb_x<a->mb_width2; mb_x++){
  263. dct_get(a, mb_x, mb_y);
  264. encode_mb(a, a->block);
  265. }
  266. }
  267. if(a->mb_width2 != a->mb_width){
  268. mb_x= a->mb_width2;
  269. for(mb_y=0; mb_y<a->mb_height2; mb_y++){
  270. dct_get(a, mb_x, mb_y);
  271. encode_mb(a, a->block);
  272. }
  273. }
  274. if(a->mb_height2 != a->mb_height){
  275. mb_y= a->mb_height2;
  276. for(mb_x=0; mb_x<a->mb_width; mb_x++){
  277. dct_get(a, mb_x, mb_y);
  278. encode_mb(a, a->block);
  279. }
  280. }
  281. emms_c();
  282. align_put_bits(&a->pb);
  283. while(get_bit_count(&a->pb)&31)
  284. put_bits(&a->pb, 8, 0);
  285. size= get_bit_count(&a->pb)/32;
  286. a->dsp.bswap_buf((uint32_t*)buf, (uint32_t*)buf, size);
  287. return size*4;
  288. }
  289. static void common_init(AVCodecContext *avctx){
  290. ASV1Context * const a = avctx->priv_data;
  291. dsputil_init(&a->dsp, avctx);
  292. a->mb_width = (avctx->width + 15) / 16;
  293. a->mb_height = (avctx->height + 15) / 16;
  294. a->mb_width2 = (avctx->width + 0) / 16;
  295. a->mb_height2 = (avctx->height + 0) / 16;
  296. avctx->coded_frame= (AVFrame*)&a->picture;
  297. a->avctx= avctx;
  298. }
  299. static int decode_init(AVCodecContext *avctx){
  300. ASV1Context * const a = avctx->priv_data;
  301. AVFrame *p= (AVFrame*)&a->picture;
  302. int i;
  303. common_init(avctx);
  304. init_vlcs(a);
  305. ff_init_scantable(a->dsp.idct_permutation, &a->scantable, scantab);
  306. a->inv_qscale= le2me_32(((uint32_t*)avctx->extradata)[0]);
  307. if(a->inv_qscale == 0){
  308. printf("illegal qscale 0\n");
  309. a->inv_qscale= 6;
  310. }
  311. for(i=0; i<64; i++){
  312. int index= scantab[i];
  313. a->intra_matrix[i]= 64*ff_mpeg1_default_intra_matrix[index] / a->inv_qscale;
  314. }
  315. p->qstride= a->mb_width;
  316. p->qscale_table= av_mallocz( p->qstride * a->mb_height);
  317. return 0;
  318. }
  319. static int encode_init(AVCodecContext *avctx){
  320. ASV1Context * const a = avctx->priv_data;
  321. int i;
  322. common_init(avctx);
  323. if(avctx->global_quality == 0) avctx->global_quality= 4*FF_QUALITY_SCALE;
  324. a->inv_qscale= (32*FF_QUALITY_SCALE + avctx->global_quality/2) / avctx->global_quality;
  325. avctx->extradata= av_mallocz(8);
  326. avctx->extradata_size=8;
  327. ((uint32_t*)avctx->extradata)[0]= le2me_32(a->inv_qscale);
  328. ((uint32_t*)avctx->extradata)[1]= le2me_32(ff_get_fourcc("ASUS"));
  329. for(i=0; i<64; i++){
  330. int q= 32*ff_mpeg1_default_intra_matrix[i];
  331. a->q_intra_matrix[i]= ((a->inv_qscale<<16) + q/2) / q;
  332. }
  333. return 0;
  334. }
  335. static int decode_end(AVCodecContext *avctx){
  336. ASV1Context * const a = avctx->priv_data;
  337. av_freep(&a->bitstream_buffer);
  338. av_freep(&a->picture.qscale_table);
  339. a->bitstream_buffer_size=0;
  340. avcodec_default_free_buffers(avctx);
  341. return 0;
  342. }
  343. AVCodec asv1_decoder = {
  344. "asv1",
  345. CODEC_TYPE_VIDEO,
  346. CODEC_ID_ASV1,
  347. sizeof(ASV1Context),
  348. decode_init,
  349. NULL,
  350. decode_end,
  351. decode_frame,
  352. CODEC_CAP_DR1,
  353. };
  354. #ifdef CONFIG_ENCODERS
  355. AVCodec asv1_encoder = {
  356. "asv1",
  357. CODEC_TYPE_VIDEO,
  358. CODEC_ID_ASV1,
  359. sizeof(ASV1Context),
  360. encode_init,
  361. encode_frame,
  362. //encode_end,
  363. };
  364. #endif //CONFIG_ENCODERS