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.

293 lines
8.5KB

  1. /*
  2. * PSX MDEC 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. * based upon code from Sebastian Jedruszkiewicz <elf@frogger.rules.pl>
  20. */
  21. /**
  22. * @file mdec.c
  23. * PSX MDEC codec.
  24. * This is very similar to intra only MPEG1.
  25. */
  26. #include "avcodec.h"
  27. #include "dsputil.h"
  28. #include "mpegvideo.h"
  29. //#undef NDEBUG
  30. //#include <assert.h>
  31. typedef struct MDECContext{
  32. AVCodecContext *avctx;
  33. DSPContext dsp;
  34. AVFrame picture;
  35. PutBitContext pb;
  36. GetBitContext gb;
  37. ScanTable scantable;
  38. int version;
  39. int qscale;
  40. int last_dc[3];
  41. int mb_width;
  42. int mb_height;
  43. int mb_x, mb_y;
  44. DCTELEM __align8 block[6][64];
  45. uint16_t __align8 intra_matrix[64];
  46. int __align8 q_intra_matrix[64];
  47. uint8_t *bitstream_buffer;
  48. int bitstream_buffer_size;
  49. int block_last_index[6];
  50. } MDECContext;
  51. //very similar to mpeg1
  52. static inline int mdec_decode_block_intra(MDECContext *a, DCTELEM *block, int n)
  53. {
  54. int level, diff, i, j, run;
  55. int component;
  56. RLTable *rl = &rl_mpeg1;
  57. uint8_t * const scantable= a->scantable.permutated;
  58. const uint16_t *quant_matrix= ff_mpeg1_default_intra_matrix;
  59. const int qscale= a->qscale;
  60. /* DC coef */
  61. if(a->version==2){
  62. block[0]= get_sbits(&a->gb, 11);
  63. }else{
  64. component = (n <= 3 ? 0 : n - 4 + 1);
  65. diff = decode_dc(&a->gb, component);
  66. if (diff >= 0xffff)
  67. return -1;
  68. a->last_dc[component]+= diff;
  69. block[0] = a->last_dc[component]<<3;
  70. }
  71. i = 0;
  72. {
  73. OPEN_READER(re, &a->gb);
  74. /* now quantify & encode AC coefs */
  75. for(;;) {
  76. UPDATE_CACHE(re, &a->gb);
  77. GET_RL_VLC(level, run, re, &a->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  78. if(level == 127){
  79. break;
  80. } else if(level != 0) {
  81. i += run;
  82. j = scantable[i];
  83. level= (level*qscale*quant_matrix[j])>>3;
  84. level= (level-1)|1;
  85. level = (level ^ SHOW_SBITS(re, &a->gb, 1)) - SHOW_SBITS(re, &a->gb, 1);
  86. LAST_SKIP_BITS(re, &a->gb, 1);
  87. } else {
  88. /* escape */
  89. run = SHOW_UBITS(re, &a->gb, 6)+1; LAST_SKIP_BITS(re, &a->gb, 6);
  90. UPDATE_CACHE(re, &a->gb);
  91. level = SHOW_SBITS(re, &a->gb, 8); SKIP_BITS(re, &a->gb, 8);
  92. if (level == -128) {
  93. level = SHOW_UBITS(re, &a->gb, 8) - 256; LAST_SKIP_BITS(re, &a->gb, 8);
  94. } else if (level == 0) {
  95. level = SHOW_UBITS(re, &a->gb, 8) ; LAST_SKIP_BITS(re, &a->gb, 8);
  96. }
  97. i += run;
  98. j = scantable[i];
  99. /* if(level<0){
  100. level= -level;
  101. level= (level*qscale*quant_matrix[j])>>4;
  102. level= (level-1)|1;
  103. level= -level;
  104. }else{*/
  105. level= (level*qscale*quant_matrix[j])>>4;
  106. /* level= (level-1)|1;
  107. }*/
  108. }
  109. if (i > 63){
  110. fprintf(stderr, "ac-tex damaged at %d %d\n", a->mb_x, a->mb_y);
  111. return -1;
  112. }
  113. block[j] = level;
  114. }
  115. CLOSE_READER(re, &a->gb);
  116. }
  117. a->block_last_index[n] = i;
  118. return 0;
  119. }
  120. static inline int decode_mb(MDECContext *a, DCTELEM block[6][64]){
  121. int i;
  122. a->dsp.clear_blocks(block[0]);
  123. for(i=5; i>=0; i--){
  124. if( mdec_decode_block_intra(a, block[i], i) < 0)
  125. return -1;
  126. }
  127. return 0;
  128. }
  129. static inline void idct_put(MDECContext *a, int mb_x, int mb_y){
  130. DCTELEM (*block)[64]= a->block;
  131. int linesize= a->picture.linesize[0];
  132. uint8_t *dest_y = a->picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
  133. uint8_t *dest_cb = a->picture.data[1] + (mb_y * 8 * a->picture.linesize[1]) + mb_x * 8;
  134. uint8_t *dest_cr = a->picture.data[2] + (mb_y * 8 * a->picture.linesize[2]) + mb_x * 8;
  135. a->dsp.idct_put(dest_y , linesize, block[0]);
  136. a->dsp.idct_put(dest_y + 8, linesize, block[1]);
  137. a->dsp.idct_put(dest_y + 8*linesize , linesize, block[2]);
  138. a->dsp.idct_put(dest_y + 8*linesize + 8, linesize, block[3]);
  139. if(!(a->avctx->flags&CODEC_FLAG_GRAY)){
  140. a->dsp.idct_put(dest_cb, a->picture.linesize[1], block[4]);
  141. a->dsp.idct_put(dest_cr, a->picture.linesize[2], block[5]);
  142. }
  143. }
  144. static int decode_frame(AVCodecContext *avctx,
  145. void *data, int *data_size,
  146. uint8_t *buf, int buf_size)
  147. {
  148. MDECContext * const a = avctx->priv_data;
  149. AVFrame *picture = data;
  150. AVFrame * const p= (AVFrame*)&a->picture;
  151. int i;
  152. *data_size = 0;
  153. /* special case for last picture */
  154. if (buf_size == 0) {
  155. return 0;
  156. }
  157. if(p->data[0])
  158. avctx->release_buffer(avctx, p);
  159. p->reference= 0;
  160. if(avctx->get_buffer(avctx, p) < 0){
  161. fprintf(stderr, "get_buffer() failed\n");
  162. return -1;
  163. }
  164. p->pict_type= I_TYPE;
  165. p->key_frame= 1;
  166. a->last_dc[0]=
  167. a->last_dc[1]=
  168. a->last_dc[2]= 0;
  169. a->bitstream_buffer= av_fast_realloc(a->bitstream_buffer, &a->bitstream_buffer_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  170. for(i=0; 2*i<buf_size; i+=2){
  171. ((uint16_t*)a->bitstream_buffer)[i] = ((uint16_t*)buf)[i+1];
  172. ((uint16_t*)a->bitstream_buffer)[i+1]= ((uint16_t*)buf)[i ];
  173. }
  174. // a->dsp.bswap_buf((uint32_t*)a->bitstream_buffer, (uint32_t*)buf, buf_size/4);
  175. init_get_bits(&a->gb, a->bitstream_buffer, buf_size*8);
  176. a->qscale= get_bits(&a->gb, 16);
  177. a->version= get_bits(&a->gb, 16);
  178. skip_bits(&a->gb, 8*8);
  179. printf("qscale:%d, version:%d\n", a->qscale, a->version);
  180. for(a->mb_y=0; a->mb_y<a->mb_height; a->mb_y++){
  181. for(a->mb_x=0; a->mb_x<a->mb_width; a->mb_x++){
  182. if( decode_mb(a, a->block) <0)
  183. return -1;
  184. idct_put(a, a->mb_x, a->mb_y);
  185. }
  186. }
  187. #if 0
  188. int i;
  189. printf("%d %d\n", 8*buf_size, get_bits_count(&a->gb));
  190. for(i=get_bits_count(&a->gb); i<8*buf_size; i++){
  191. printf("%d", get_bits1(&a->gb));
  192. }
  193. for(i=0; i<s->avctx->extradata_size; i++){
  194. printf("%c\n", ((uint8_t*)s->avctx->extradata)[i]);
  195. }
  196. #endif
  197. // p->quality= (32 + a->inv_qscale/2)/a->inv_qscale;
  198. // memset(p->qscale_table, p->quality, p->qstride*a->mb_height);
  199. *picture= *(AVFrame*)&a->picture;
  200. *data_size = sizeof(AVPicture);
  201. emms_c();
  202. return (get_bits_count(&a->gb)+31)/32*4;
  203. }
  204. static void mdec_common_init(AVCodecContext *avctx){
  205. MDECContext * const a = avctx->priv_data;
  206. dsputil_init(&a->dsp, avctx);
  207. a->mb_width = (avctx->width + 15) / 16;
  208. a->mb_height = (avctx->height + 15) / 16;
  209. avctx->coded_frame= (AVFrame*)&a->picture;
  210. a->avctx= avctx;
  211. }
  212. static int decode_init(AVCodecContext *avctx){
  213. MDECContext * const a = avctx->priv_data;
  214. AVFrame *p= (AVFrame*)&a->picture;
  215. int i;
  216. mdec_common_init(avctx);
  217. init_vlcs();
  218. ff_init_scantable(a->dsp.idct_permutation, &a->scantable, ff_zigzag_direct);
  219. /*
  220. for(i=0; i<64; i++){
  221. int index= ff_zigzag_direct[i];
  222. a->intra_matrix[i]= 64*ff_mpeg1_default_intra_matrix[index] / a->inv_qscale;
  223. }
  224. */
  225. p->qstride= a->mb_width;
  226. p->qscale_table= av_mallocz( p->qstride * a->mb_height);
  227. return 0;
  228. }
  229. static int decode_end(AVCodecContext *avctx){
  230. MDECContext * const a = avctx->priv_data;
  231. av_freep(&a->bitstream_buffer);
  232. av_freep(&a->picture.qscale_table);
  233. a->bitstream_buffer_size=0;
  234. avcodec_default_free_buffers(avctx);
  235. return 0;
  236. }
  237. AVCodec mdec_decoder = {
  238. "mdec",
  239. CODEC_TYPE_VIDEO,
  240. CODEC_ID_MDEC,
  241. sizeof(MDECContext),
  242. decode_init,
  243. NULL,
  244. decode_end,
  245. decode_frame,
  246. CODEC_CAP_DR1,
  247. };