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.

334 lines
11KB

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