dxa.c 11 KB

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