dxa.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Feeble Files/ScummVM DXA decoder
  3. * Copyright (c) 2007 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. /**
  23. * @file dxa.c
  24. * DXA Video decoder
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include "common.h"
  29. #include "avcodec.h"
  30. #include <zlib.h>
  31. /*
  32. * Decoder context
  33. */
  34. typedef struct DxaDecContext {
  35. AVCodecContext *avctx;
  36. AVFrame pic, prev;
  37. int dsize;
  38. uint8_t *decomp_buf;
  39. uint32_t pal[256];
  40. } DxaDecContext;
  41. static const int shift1[6] = { 0, 8, 8, 8, 4, 4 };
  42. static const int shift2[6] = { 0, 0, 8, 4, 0, 4 };
  43. static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t* dst, uint8_t *src, uint8_t *ref)
  44. {
  45. uint8_t *code, *data, *mv, *msk, *tmp, *tmp2;
  46. int i, j, k;
  47. int type, x, y, d, d2;
  48. int stride = c->pic.linesize[0];
  49. uint32_t mask;
  50. code = src + 12;
  51. data = code + ((avctx->width * avctx->height) >> 4);
  52. mv = data + AV_RB32(src + 0);
  53. msk = mv + AV_RB32(src + 4);
  54. for(j = 0; j < avctx->height; j += 4){
  55. for(i = 0; i < avctx->width; i += 4){
  56. tmp = dst + i;
  57. tmp2 = ref + i;
  58. type = *code++;
  59. switch(type){
  60. case 4: // motion compensation
  61. x = (*mv) >> 4; if(x & 8) x = 8 - x;
  62. y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
  63. tmp2 += x + y*stride;
  64. case 0: // skip
  65. case 5: // skip in method 12
  66. for(y = 0; y < 4; y++){
  67. memcpy(tmp, tmp2, 4);
  68. tmp += stride;
  69. tmp2 += stride;
  70. }
  71. break;
  72. case 1: // masked change
  73. case 10: // masked change with only half of pixels changed
  74. case 11: // cases 10-15 are for method 12 only
  75. case 12:
  76. case 13:
  77. case 14:
  78. case 15:
  79. if(type == 1){
  80. mask = AV_RB16(msk);
  81. msk += 2;
  82. }else{
  83. type -= 10;
  84. mask = ((msk[0] & 0xF0) << shift1[type]) | ((msk[0] & 0xF) << shift2[type]);
  85. msk++;
  86. }
  87. for(y = 0; y < 4; y++){
  88. for(x = 0; x < 4; x++){
  89. tmp[x] = (mask & 0x8000) ? *data++ : tmp2[x];
  90. mask <<= 1;
  91. }
  92. tmp += stride;
  93. tmp2 += stride;
  94. }
  95. break;
  96. case 2: // fill block
  97. for(y = 0; y < 4; y++){
  98. memset(tmp, data[0], 4);
  99. tmp += stride;
  100. }
  101. data++;
  102. break;
  103. case 3: // raw block
  104. for(y = 0; y < 4; y++){
  105. memcpy(tmp, data, 4);
  106. data += 4;
  107. tmp += stride;
  108. }
  109. break;
  110. case 8: // subblocks - method 13 only
  111. mask = *msk++;
  112. for(k = 0; k < 4; k++){
  113. d = ((k & 1) << 1) + ((k & 2) * stride);
  114. d2 = ((k & 1) << 1) + ((k & 2) * stride);
  115. tmp2 = ref + i + d2;
  116. switch(mask & 0xC0){
  117. case 0x80: // motion compensation
  118. x = (*mv) >> 4; if(x & 8) x = 8 - x;
  119. y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
  120. tmp2 += x + y*stride;
  121. case 0x00: // skip
  122. tmp[d + 0 ] = tmp2[0];
  123. tmp[d + 1 ] = tmp2[1];
  124. tmp[d + 0 + stride] = tmp2[0 + stride];
  125. tmp[d + 1 + stride] = tmp2[1 + stride];
  126. break;
  127. case 0x40: // fill
  128. tmp[d + 0 ] = data[0];
  129. tmp[d + 1 ] = data[0];
  130. tmp[d + 0 + stride] = data[0];
  131. tmp[d + 1 + stride] = data[0];
  132. data++;
  133. break;
  134. case 0xC0: // raw
  135. tmp[d + 0 ] = *data++;
  136. tmp[d + 1 ] = *data++;
  137. tmp[d + 0 + stride] = *data++;
  138. tmp[d + 1 + stride] = *data++;
  139. break;
  140. }
  141. mask <<= 2;
  142. }
  143. break;
  144. case 32: // vector quantization - 2 colors
  145. mask = AV_RB16(msk);
  146. msk += 2;
  147. for(y = 0; y < 4; y++){
  148. for(x = 0; x < 4; x++){
  149. tmp[x] = data[mask & 1];
  150. mask >>= 1;
  151. }
  152. tmp += stride;
  153. tmp2 += stride;
  154. }
  155. data += 2;
  156. break;
  157. case 33: // vector quantization - 3 or 4 colors
  158. case 34:
  159. mask = AV_RB32(msk);
  160. msk += 4;
  161. for(y = 0; y < 4; y++){
  162. for(x = 0; x < 4; x++){
  163. tmp[x] = data[mask & 3];
  164. mask >>= 2;
  165. }
  166. tmp += stride;
  167. tmp2 += stride;
  168. }
  169. data += type - 30;
  170. break;
  171. default:
  172. av_log(avctx, AV_LOG_ERROR, "Unknown opcode %d\n", type);
  173. return -1;
  174. }
  175. }
  176. dst += stride * 4;
  177. ref += stride * 4;
  178. }
  179. return 0;
  180. }
  181. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
  182. {
  183. DxaDecContext * const c = (DxaDecContext *)avctx->priv_data;
  184. uint8_t *outptr, *srcptr, *tmpptr;
  185. unsigned long dsize;
  186. int i, j, compr;
  187. int stride;
  188. int orig_buf_size = buf_size;
  189. int pc = 0;
  190. /* make the palette available on the way out */
  191. if(buf[0]=='C' && buf[1]=='M' && buf[2]=='A' && buf[3]=='P'){
  192. int r, g, b;
  193. buf += 4;
  194. for(i = 0; i < 256; i++){
  195. r = *buf++;
  196. g = *buf++;
  197. b = *buf++;
  198. c->pal[i] = (r << 16) | (g << 8) | b;
  199. }
  200. pc = 1;
  201. buf_size -= 768+4;
  202. }
  203. if(avctx->get_buffer(avctx, &c->pic) < 0){
  204. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  205. return -1;
  206. }
  207. memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
  208. c->pic.palette_has_changed = pc;
  209. outptr = c->pic.data[0];
  210. srcptr = c->decomp_buf;
  211. tmpptr = c->prev.data[0];
  212. stride = c->pic.linesize[0];
  213. if(buf[0]=='N' && buf[1]=='U' && buf[2]=='L' && buf[3]=='L')
  214. compr = -1;
  215. else
  216. compr = buf[4];
  217. dsize = c->dsize;
  218. if((compr != 4 && compr != -1) && uncompress(c->decomp_buf, &dsize, buf + 9, buf_size - 9) != Z_OK){
  219. av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n");
  220. return -1;
  221. }
  222. switch(compr){
  223. case -1:
  224. c->pic.key_frame = 0;
  225. c->pic.pict_type = FF_P_TYPE;
  226. if(c->prev.data[0])
  227. memcpy(c->pic.data[0], c->prev.data[0], c->pic.linesize[0] * avctx->height);
  228. else{ // Should happen only when first frame is 'NULL'
  229. memset(c->pic.data[0], 0, c->pic.linesize[0] * avctx->height);
  230. c->pic.key_frame = 1;
  231. c->pic.pict_type = FF_I_TYPE;
  232. }
  233. break;
  234. case 2:
  235. case 3:
  236. case 4:
  237. case 5:
  238. c->pic.key_frame = !(compr & 1);
  239. c->pic.pict_type = (compr & 1) ? FF_P_TYPE : FF_I_TYPE;
  240. for(j = 0; j < avctx->height; j++){
  241. if(compr & 1){
  242. for(i = 0; i < avctx->width; i++)
  243. outptr[i] = srcptr[i] ^ tmpptr[i];
  244. tmpptr += stride;
  245. }else
  246. memcpy(outptr, srcptr, avctx->width);
  247. outptr += stride;
  248. srcptr += avctx->width;
  249. }
  250. break;
  251. case 12: // ScummVM coding
  252. case 13:
  253. c->pic.key_frame = 0;
  254. c->pic.pict_type = FF_P_TYPE;
  255. decode_13(avctx, c, c->pic.data[0], srcptr, c->prev.data[0]);
  256. break;
  257. default:
  258. av_log(avctx, AV_LOG_ERROR, "Unknown/unsupported compression type %d\n", buf[4]);
  259. return -1;
  260. }
  261. FFSWAP(AVFrame, c->pic, c->prev);
  262. if(c->pic.data[0])
  263. avctx->release_buffer(avctx, &c->pic);
  264. *data_size = sizeof(AVFrame);
  265. *(AVFrame*)data = c->prev;
  266. /* always report that the buffer was completely consumed */
  267. return orig_buf_size;
  268. }
  269. static int decode_init(AVCodecContext *avctx)
  270. {
  271. DxaDecContext * const c = (DxaDecContext *)avctx->priv_data;
  272. c->avctx = avctx;
  273. avctx->pix_fmt = PIX_FMT_PAL8;
  274. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
  275. return -1;
  276. }
  277. c->dsize = avctx->width * avctx->height * 2;
  278. if((c->decomp_buf = av_malloc(c->dsize)) == NULL) {
  279. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  280. return -1;
  281. }
  282. return 0;
  283. }
  284. static int decode_end(AVCodecContext *avctx)
  285. {
  286. DxaDecContext * const c = (DxaDecContext *)avctx->priv_data;
  287. av_freep(&c->decomp_buf);
  288. if(c->prev.data[0])
  289. avctx->release_buffer(avctx, &c->prev);
  290. if(c->pic.data[0])
  291. avctx->release_buffer(avctx, &c->pic);
  292. return 0;
  293. }
  294. AVCodec dxa_decoder = {
  295. "dxa",
  296. CODEC_TYPE_VIDEO,
  297. CODEC_ID_DXA,
  298. sizeof(DxaDecContext),
  299. decode_init,
  300. NULL,
  301. decode_end,
  302. decode_frame
  303. };