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.

333 lines
10KB

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