dxa.c 10 KB

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