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.

329 lines
10KB

  1. /*
  2. * Feeble Files/ScummVM DXA decoder
  3. * Copyright (c) 2007 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * DXA Video decoder
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include "libavutil/intreadwrite.h"
  28. #include "avcodec.h"
  29. #include <zlib.h>
  30. /*
  31. * Decoder context
  32. */
  33. typedef struct DxaDecContext {
  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, AVPacket *avpkt)
  180. {
  181. const uint8_t *buf = avpkt->data;
  182. int buf_size = avpkt->size;
  183. DxaDecContext * const c = 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 = AV_PICTURE_TYPE_P;
  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 = AV_PICTURE_TYPE_I;
  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) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  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 = AV_PICTURE_TYPE_P;
  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 av_cold int decode_init(AVCodecContext *avctx)
  270. {
  271. DxaDecContext * const c = avctx->priv_data;
  272. avctx->pix_fmt = PIX_FMT_PAL8;
  273. c->dsize = avctx->width * avctx->height * 2;
  274. if((c->decomp_buf = av_malloc(c->dsize)) == NULL) {
  275. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  276. return -1;
  277. }
  278. return 0;
  279. }
  280. static av_cold int decode_end(AVCodecContext *avctx)
  281. {
  282. DxaDecContext * const c = avctx->priv_data;
  283. av_freep(&c->decomp_buf);
  284. if(c->prev.data[0])
  285. avctx->release_buffer(avctx, &c->prev);
  286. if(c->pic.data[0])
  287. avctx->release_buffer(avctx, &c->pic);
  288. return 0;
  289. }
  290. AVCodec ff_dxa_decoder = {
  291. .name = "dxa",
  292. .type = AVMEDIA_TYPE_VIDEO,
  293. .id = CODEC_ID_DXA,
  294. .priv_data_size = sizeof(DxaDecContext),
  295. .init = decode_init,
  296. .close = decode_end,
  297. .decode = decode_frame,
  298. .capabilities = CODEC_CAP_DR1,
  299. .long_name = NULL_IF_CONFIG_SMALL("Feeble Files/ScummVM DXA"),
  300. };