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.

273 lines
7.9KB

  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]= 2*get_sbits(&a->gb, 10) + 1024;
  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, 10); SKIP_BITS(re, &a->gb, 10);
  92. i += run;
  93. j = scantable[i];
  94. if(level<0){
  95. level= -level;
  96. level= (level*qscale*quant_matrix[j])>>3;
  97. level= (level-1)|1;
  98. level= -level;
  99. }else{
  100. level= (level*qscale*quant_matrix[j])>>3;
  101. level= (level-1)|1;
  102. }
  103. }
  104. if (i > 63){
  105. av_log(a->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", a->mb_x, a->mb_y);
  106. return -1;
  107. }
  108. block[j] = level;
  109. }
  110. CLOSE_READER(re, &a->gb);
  111. }
  112. a->block_last_index[n] = i;
  113. return 0;
  114. }
  115. static inline int decode_mb(MDECContext *a, DCTELEM block[6][64]){
  116. int i;
  117. const int block_index[6]= {5,4,0,1,2,3};
  118. a->dsp.clear_blocks(block[0]);
  119. for(i=0; i<6; i++){
  120. if( mdec_decode_block_intra(a, block[ block_index[i] ], block_index[i]) < 0)
  121. return -1;
  122. }
  123. return 0;
  124. }
  125. static inline void idct_put(MDECContext *a, int mb_x, int mb_y){
  126. DCTELEM (*block)[64]= a->block;
  127. int linesize= a->picture.linesize[0];
  128. uint8_t *dest_y = a->picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
  129. uint8_t *dest_cb = a->picture.data[1] + (mb_y * 8 * a->picture.linesize[1]) + mb_x * 8;
  130. uint8_t *dest_cr = a->picture.data[2] + (mb_y * 8 * a->picture.linesize[2]) + mb_x * 8;
  131. a->dsp.idct_put(dest_y , linesize, block[0]);
  132. a->dsp.idct_put(dest_y + 8, linesize, block[1]);
  133. a->dsp.idct_put(dest_y + 8*linesize , linesize, block[2]);
  134. a->dsp.idct_put(dest_y + 8*linesize + 8, linesize, block[3]);
  135. if(!(a->avctx->flags&CODEC_FLAG_GRAY)){
  136. a->dsp.idct_put(dest_cb, a->picture.linesize[1], block[4]);
  137. a->dsp.idct_put(dest_cr, a->picture.linesize[2], block[5]);
  138. }
  139. }
  140. static int decode_frame(AVCodecContext *avctx,
  141. void *data, int *data_size,
  142. uint8_t *buf, int buf_size)
  143. {
  144. MDECContext * const a = avctx->priv_data;
  145. AVFrame *picture = data;
  146. AVFrame * const p= (AVFrame*)&a->picture;
  147. int i;
  148. /* special case for last picture */
  149. if (buf_size == 0) {
  150. return 0;
  151. }
  152. if(p->data[0])
  153. avctx->release_buffer(avctx, p);
  154. p->reference= 0;
  155. if(avctx->get_buffer(avctx, p) < 0){
  156. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  157. return -1;
  158. }
  159. p->pict_type= I_TYPE;
  160. p->key_frame= 1;
  161. a->last_dc[0]=
  162. a->last_dc[1]=
  163. a->last_dc[2]= 0;
  164. a->bitstream_buffer= av_fast_realloc(a->bitstream_buffer, &a->bitstream_buffer_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  165. for(i=0; i<buf_size; i+=2){
  166. a->bitstream_buffer[i] = buf[i+1];
  167. a->bitstream_buffer[i+1]= buf[i ];
  168. }
  169. init_get_bits(&a->gb, a->bitstream_buffer, buf_size*8);
  170. /* skip over 4 preamble bytes in stream (typically 0xXX 0xXX 0x00 0x38) */
  171. skip_bits(&a->gb, 32);
  172. a->qscale= get_bits(&a->gb, 16);
  173. a->version= get_bits(&a->gb, 16);
  174. // printf("qscale:%d (0x%X), version:%d (0x%X)\n", a->qscale, a->qscale, a->version, a->version);
  175. for(a->mb_x=0; a->mb_x<a->mb_width; a->mb_x++){
  176. for(a->mb_y=0; a->mb_y<a->mb_height; a->mb_y++){
  177. if( decode_mb(a, a->block) <0)
  178. return -1;
  179. idct_put(a, a->mb_x, a->mb_y);
  180. }
  181. }
  182. // p->quality= (32 + a->inv_qscale/2)/a->inv_qscale;
  183. // memset(p->qscale_table, p->quality, p->qstride*a->mb_height);
  184. *picture= *(AVFrame*)&a->picture;
  185. *data_size = sizeof(AVPicture);
  186. emms_c();
  187. return (get_bits_count(&a->gb)+31)/32*4;
  188. }
  189. static void mdec_common_init(AVCodecContext *avctx){
  190. MDECContext * const a = avctx->priv_data;
  191. dsputil_init(&a->dsp, avctx);
  192. a->mb_width = (avctx->width + 15) / 16;
  193. a->mb_height = (avctx->height + 15) / 16;
  194. avctx->coded_frame= (AVFrame*)&a->picture;
  195. a->avctx= avctx;
  196. }
  197. static int decode_init(AVCodecContext *avctx){
  198. MDECContext * const a = avctx->priv_data;
  199. AVFrame *p= (AVFrame*)&a->picture;
  200. mdec_common_init(avctx);
  201. init_vlcs();
  202. ff_init_scantable(a->dsp.idct_permutation, &a->scantable, ff_zigzag_direct);
  203. /*
  204. for(i=0; i<64; i++){
  205. int index= ff_zigzag_direct[i];
  206. a->intra_matrix[i]= 64*ff_mpeg1_default_intra_matrix[index] / a->inv_qscale;
  207. }
  208. */
  209. p->qstride= a->mb_width;
  210. p->qscale_table= av_mallocz( p->qstride * a->mb_height);
  211. return 0;
  212. }
  213. static int decode_end(AVCodecContext *avctx){
  214. MDECContext * const a = avctx->priv_data;
  215. av_freep(&a->bitstream_buffer);
  216. av_freep(&a->picture.qscale_table);
  217. a->bitstream_buffer_size=0;
  218. return 0;
  219. }
  220. AVCodec mdec_decoder = {
  221. "mdec",
  222. CODEC_TYPE_VIDEO,
  223. CODEC_ID_MDEC,
  224. sizeof(MDECContext),
  225. decode_init,
  226. NULL,
  227. decode_end,
  228. decode_frame,
  229. CODEC_CAP_DR1,
  230. };