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.

354 lines
11KB

  1. /*
  2. * Fraps FPS1 decoder
  3. * Copyright (c) 2005 Roine Gustafsson
  4. * Copyright (c) 2006 Konstantin Shishkov
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Lossless Fraps 'FPS1' decoder
  25. * @author Roine Gustafsson (roine at users sf net)
  26. * @author Konstantin Shishkov
  27. *
  28. * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
  29. *
  30. * Version 2 files support by Konstantin Shishkov
  31. */
  32. #define UNCHECKED_BITSTREAM_READER 1
  33. #include "avcodec.h"
  34. #include "get_bits.h"
  35. #include "huffman.h"
  36. #include "bytestream.h"
  37. #include "bswapdsp.h"
  38. #include "internal.h"
  39. #include "thread.h"
  40. #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
  41. #define VLC_BITS 11
  42. /**
  43. * local variable storage
  44. */
  45. typedef struct FrapsContext {
  46. AVCodecContext *avctx;
  47. BswapDSPContext bdsp;
  48. uint8_t *tmpbuf;
  49. int tmpbuf_size;
  50. } FrapsContext;
  51. /**
  52. * initializes decoder
  53. * @param avctx codec context
  54. * @return 0 on success or negative if fails
  55. */
  56. static av_cold int decode_init(AVCodecContext *avctx)
  57. {
  58. FrapsContext * const s = avctx->priv_data;
  59. s->avctx = avctx;
  60. s->tmpbuf = NULL;
  61. ff_bswapdsp_init(&s->bdsp);
  62. return 0;
  63. }
  64. /**
  65. * Comparator - our nodes should ascend by count
  66. * but with preserved symbol order
  67. */
  68. static int huff_cmp(const void *va, const void *vb)
  69. {
  70. const Node *a = va, *b = vb;
  71. return (a->count - b->count)*256 + a->sym - b->sym;
  72. }
  73. /**
  74. * decode Fraps v2 packed plane
  75. */
  76. static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
  77. int h, const uint8_t *src, int size, int Uoff,
  78. const int step)
  79. {
  80. int i, j, ret;
  81. GetBitContext gb;
  82. VLC vlc;
  83. Node nodes[512];
  84. for (i = 0; i < 256; i++)
  85. nodes[i].count = bytestream_get_le32(&src);
  86. size -= 1024;
  87. if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, VLC_BITS,
  88. nodes, huff_cmp,
  89. FF_HUFFMAN_FLAG_ZERO_COUNT)) < 0)
  90. return ret;
  91. /* we have built Huffman table and are ready to decode plane */
  92. /* convert bits so they may be used by standard bitreader */
  93. s->bdsp.bswap_buf((uint32_t *) s->tmpbuf,
  94. (const uint32_t *) src, size >> 2);
  95. if ((ret = init_get_bits8(&gb, s->tmpbuf, size)) < 0)
  96. return ret;
  97. for (j = 0; j < h; j++) {
  98. for (i = 0; i < w*step; i += step) {
  99. dst[i] = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
  100. /* lines are stored as deltas between previous lines
  101. * and we need to add 0x80 to the first lines of chroma planes
  102. */
  103. if (j)
  104. dst[i] += dst[i - stride];
  105. else if (Uoff)
  106. dst[i] += 0x80;
  107. if (get_bits_left(&gb) < 0) {
  108. ff_free_vlc(&vlc);
  109. return AVERROR_INVALIDDATA;
  110. }
  111. }
  112. dst += stride;
  113. }
  114. ff_free_vlc(&vlc);
  115. return 0;
  116. }
  117. static int decode_frame(AVCodecContext *avctx,
  118. void *data, int *got_frame,
  119. AVPacket *avpkt)
  120. {
  121. FrapsContext * const s = avctx->priv_data;
  122. const uint8_t *buf = avpkt->data;
  123. int buf_size = avpkt->size;
  124. ThreadFrame frame = { .f = data };
  125. AVFrame * const f = data;
  126. uint32_t header;
  127. unsigned int version,header_size;
  128. unsigned int x, y;
  129. const uint32_t *buf32;
  130. uint32_t *luma1,*luma2,*cb,*cr;
  131. uint32_t offs[4];
  132. int i, j, ret, is_chroma;
  133. const int planes = 3;
  134. int is_pal;
  135. uint8_t *out;
  136. if (buf_size < 4) {
  137. av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
  138. return AVERROR_INVALIDDATA;
  139. }
  140. header = AV_RL32(buf);
  141. version = header & 0xff;
  142. is_pal = buf[1] == 2 && version == 1;
  143. header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
  144. if (version > 5) {
  145. avpriv_report_missing_feature(avctx, "Fraps version %u", version);
  146. return AVERROR_PATCHWELCOME;
  147. }
  148. buf += header_size;
  149. if (is_pal) {
  150. unsigned needed_size = avctx->width * avctx->height + 1024;
  151. needed_size += header_size;
  152. if (buf_size != needed_size) {
  153. av_log(avctx, AV_LOG_ERROR,
  154. "Invalid frame length %d (should be %d)\n",
  155. buf_size, needed_size);
  156. return AVERROR_INVALIDDATA;
  157. }
  158. } else if (version < 2) {
  159. unsigned needed_size = avctx->width * avctx->height * 3;
  160. if (version == 0) needed_size /= 2;
  161. needed_size += header_size;
  162. /* bit 31 means same as previous pic */
  163. if (header & (1U<<31)) {
  164. *got_frame = 0;
  165. return buf_size;
  166. }
  167. if (buf_size != needed_size) {
  168. av_log(avctx, AV_LOG_ERROR,
  169. "Invalid frame length %d (should be %d)\n",
  170. buf_size, needed_size);
  171. return AVERROR_INVALIDDATA;
  172. }
  173. } else {
  174. /* skip frame */
  175. if (buf_size == 8) {
  176. *got_frame = 0;
  177. return buf_size;
  178. }
  179. if (AV_RL32(buf) != FPS_TAG || buf_size < planes*1024 + 24) {
  180. av_log(avctx, AV_LOG_ERROR, "error in data stream\n");
  181. return AVERROR_INVALIDDATA;
  182. }
  183. for (i = 0; i < planes; i++) {
  184. offs[i] = AV_RL32(buf + 4 + i * 4);
  185. if (offs[i] >= buf_size - header_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  186. av_log(avctx, AV_LOG_ERROR, "plane %i offset is out of bounds\n", i);
  187. return AVERROR_INVALIDDATA;
  188. }
  189. }
  190. offs[planes] = buf_size - header_size;
  191. for (i = 0; i < planes; i++) {
  192. av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024);
  193. if (!s->tmpbuf)
  194. return AVERROR(ENOMEM);
  195. }
  196. }
  197. f->pict_type = AV_PICTURE_TYPE_I;
  198. f->key_frame = 1;
  199. avctx->pix_fmt = version & 1 ? is_pal ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
  200. avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED
  201. : AVCOL_RANGE_JPEG;
  202. avctx->colorspace = version & 1 ? AVCOL_SPC_UNSPECIFIED : AVCOL_SPC_BT709;
  203. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  204. return ret;
  205. switch (version) {
  206. case 0:
  207. default:
  208. /* Fraps v0 is a reordered YUV420 */
  209. if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
  210. av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
  211. avctx->width, avctx->height);
  212. return AVERROR_INVALIDDATA;
  213. }
  214. buf32 = (const uint32_t*)buf;
  215. for (y = 0; y < avctx->height / 2; y++) {
  216. luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0] ];
  217. luma2 = (uint32_t*)&f->data[0][ (y * 2 + 1) * f->linesize[0] ];
  218. cr = (uint32_t*)&f->data[1][ y * f->linesize[1] ];
  219. cb = (uint32_t*)&f->data[2][ y * f->linesize[2] ];
  220. for (x = 0; x < avctx->width; x += 8) {
  221. *luma1++ = *buf32++;
  222. *luma1++ = *buf32++;
  223. *luma2++ = *buf32++;
  224. *luma2++ = *buf32++;
  225. *cr++ = *buf32++;
  226. *cb++ = *buf32++;
  227. }
  228. }
  229. break;
  230. case 1:
  231. if (is_pal) {
  232. uint32_t *pal = (uint32_t *)f->data[1];
  233. for (y = 0; y < 256; y++) {
  234. pal[y] = AV_RL32(buf) | 0xFF000000;
  235. buf += 4;
  236. }
  237. for (y = 0; y <avctx->height; y++)
  238. memcpy(&f->data[0][y * f->linesize[0]],
  239. &buf[y * avctx->width],
  240. avctx->width);
  241. } else {
  242. /* Fraps v1 is an upside-down BGR24 */
  243. for (y = 0; y<avctx->height; y++)
  244. memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
  245. &buf[y * avctx->width * 3],
  246. 3 * avctx->width);
  247. }
  248. break;
  249. case 2:
  250. case 4:
  251. /**
  252. * Fraps v2 is Huffman-coded YUV420 planes
  253. * Fraps v4 is virtually the same
  254. */
  255. for (i = 0; i < planes; i++) {
  256. is_chroma = !!i;
  257. if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
  258. avctx->width >> is_chroma,
  259. avctx->height >> is_chroma,
  260. buf + offs[i], offs[i + 1] - offs[i],
  261. is_chroma, 1)) < 0) {
  262. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  263. return ret;
  264. }
  265. }
  266. break;
  267. case 3:
  268. case 5:
  269. /* Virtually the same as version 4, but is for RGB24 */
  270. for (i = 0; i < planes; i++) {
  271. if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
  272. -f->linesize[0], avctx->width, avctx->height,
  273. buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
  274. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  275. return ret;
  276. }
  277. }
  278. out = f->data[0];
  279. // convert pseudo-YUV into real RGB
  280. for (j = 0; j < avctx->height; j++) {
  281. uint8_t *line_end = out + 3*avctx->width;
  282. while (out < line_end) {
  283. out[0] += out[1];
  284. out[2] += out[1];
  285. out += 3;
  286. }
  287. out += f->linesize[0] - 3*avctx->width;
  288. }
  289. break;
  290. }
  291. *got_frame = 1;
  292. return buf_size;
  293. }
  294. /**
  295. * closes decoder
  296. * @param avctx codec context
  297. * @return 0 on success or negative if fails
  298. */
  299. static av_cold int decode_end(AVCodecContext *avctx)
  300. {
  301. FrapsContext *s = (FrapsContext*)avctx->priv_data;
  302. av_freep(&s->tmpbuf);
  303. return 0;
  304. }
  305. AVCodec ff_fraps_decoder = {
  306. .name = "fraps",
  307. .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
  308. .type = AVMEDIA_TYPE_VIDEO,
  309. .id = AV_CODEC_ID_FRAPS,
  310. .priv_data_size = sizeof(FrapsContext),
  311. .init = decode_init,
  312. .close = decode_end,
  313. .decode = decode_frame,
  314. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  315. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  316. };