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.

327 lines
10KB

  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. #include "avcodec.h"
  33. #include "get_bits.h"
  34. #include "huffman.h"
  35. #include "bytestream.h"
  36. #include "bswapdsp.h"
  37. #include "internal.h"
  38. #include "thread.h"
  39. #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
  40. #define VLC_BITS 11
  41. /**
  42. * local variable storage
  43. */
  44. typedef struct FrapsContext {
  45. AVCodecContext *avctx;
  46. BswapDSPContext bdsp;
  47. uint8_t *tmpbuf;
  48. int tmpbuf_size;
  49. } FrapsContext;
  50. /**
  51. * initializes decoder
  52. * @param avctx codec context
  53. * @return 0 on success or negative if fails
  54. */
  55. static av_cold int decode_init(AVCodecContext *avctx)
  56. {
  57. FrapsContext * const s = avctx->priv_data;
  58. s->avctx = avctx;
  59. s->tmpbuf = NULL;
  60. ff_bswapdsp_init(&s->bdsp);
  61. return 0;
  62. }
  63. /**
  64. * Comparator - our nodes should ascend by count
  65. * but with preserved symbol order
  66. */
  67. static int huff_cmp(const void *va, const void *vb)
  68. {
  69. const Node *a = va, *b = vb;
  70. return (a->count - b->count)*256 + a->sym - b->sym;
  71. }
  72. /**
  73. * decode Fraps v2 packed plane
  74. */
  75. static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
  76. int h, const uint8_t *src, int size, int Uoff,
  77. const int step)
  78. {
  79. int i, j, ret;
  80. GetBitContext gb;
  81. VLC vlc;
  82. Node nodes[512];
  83. for (i = 0; i < 256; i++)
  84. nodes[i].count = bytestream_get_le32(&src);
  85. size -= 1024;
  86. if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, VLC_BITS,
  87. nodes, huff_cmp,
  88. FF_HUFFMAN_FLAG_ZERO_COUNT)) < 0)
  89. return ret;
  90. /* we have built Huffman table and are ready to decode plane */
  91. /* convert bits so they may be used by standard bitreader */
  92. s->bdsp.bswap_buf((uint32_t *) s->tmpbuf,
  93. (const uint32_t *) src, size >> 2);
  94. init_get_bits(&gb, s->tmpbuf, size * 8);
  95. for (j = 0; j < h; j++) {
  96. for (i = 0; i < w*step; i += step) {
  97. dst[i] = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
  98. /* lines are stored as deltas between previous lines
  99. * and we need to add 0x80 to the first lines of chroma planes
  100. */
  101. if (j)
  102. dst[i] += dst[i - stride];
  103. else if (Uoff)
  104. dst[i] += 0x80;
  105. if (get_bits_left(&gb) < 0) {
  106. ff_free_vlc(&vlc);
  107. return AVERROR_INVALIDDATA;
  108. }
  109. }
  110. dst += stride;
  111. }
  112. ff_free_vlc(&vlc);
  113. return 0;
  114. }
  115. static int decode_frame(AVCodecContext *avctx,
  116. void *data, int *got_frame,
  117. AVPacket *avpkt)
  118. {
  119. FrapsContext * const s = avctx->priv_data;
  120. const uint8_t *buf = avpkt->data;
  121. int buf_size = avpkt->size;
  122. ThreadFrame frame = { .f = data };
  123. AVFrame * const f = data;
  124. uint32_t header;
  125. unsigned int version,header_size;
  126. unsigned int x, y;
  127. const uint32_t *buf32;
  128. uint32_t *luma1,*luma2,*cb,*cr;
  129. uint32_t offs[4];
  130. int i, j, ret, is_chroma;
  131. const int planes = 3;
  132. uint8_t *out;
  133. if (buf_size < 4) {
  134. av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
  135. return AVERROR_INVALIDDATA;
  136. }
  137. header = AV_RL32(buf);
  138. version = header & 0xff;
  139. header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
  140. if (version > 5) {
  141. av_log(avctx, AV_LOG_ERROR,
  142. "This file is encoded with Fraps version %d. " \
  143. "This codec can only decode versions <= 5.\n", version);
  144. return AVERROR_PATCHWELCOME;
  145. }
  146. buf += header_size;
  147. if (version < 2) {
  148. unsigned needed_size = avctx->width * avctx->height * 3;
  149. if (version == 0) needed_size /= 2;
  150. needed_size += header_size;
  151. /* bit 31 means same as previous pic */
  152. if (header & (1U<<31)) {
  153. *got_frame = 0;
  154. return buf_size;
  155. }
  156. if (buf_size != needed_size) {
  157. av_log(avctx, AV_LOG_ERROR,
  158. "Invalid frame length %d (should be %d)\n",
  159. buf_size, needed_size);
  160. return AVERROR_INVALIDDATA;
  161. }
  162. } else {
  163. /* skip frame */
  164. if (buf_size == 8) {
  165. *got_frame = 0;
  166. return buf_size;
  167. }
  168. if (AV_RL32(buf) != FPS_TAG || buf_size < planes*1024 + 24) {
  169. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  170. return AVERROR_INVALIDDATA;
  171. }
  172. for (i = 0; i < planes; i++) {
  173. offs[i] = AV_RL32(buf + 4 + i * 4);
  174. if (offs[i] >= buf_size - header_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  175. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  176. return AVERROR_INVALIDDATA;
  177. }
  178. }
  179. offs[planes] = buf_size - header_size;
  180. for (i = 0; i < planes; i++) {
  181. av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024);
  182. if (!s->tmpbuf)
  183. return AVERROR(ENOMEM);
  184. }
  185. }
  186. f->pict_type = AV_PICTURE_TYPE_I;
  187. f->key_frame = 1;
  188. avctx->pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
  189. avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED
  190. : AVCOL_RANGE_JPEG;
  191. avctx->colorspace = version & 1 ? AVCOL_SPC_UNSPECIFIED : AVCOL_SPC_BT709;
  192. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  193. return ret;
  194. switch (version) {
  195. case 0:
  196. default:
  197. /* Fraps v0 is a reordered YUV420 */
  198. if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
  199. av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
  200. avctx->width, avctx->height);
  201. return AVERROR_INVALIDDATA;
  202. }
  203. buf32 = (const uint32_t*)buf;
  204. for (y = 0; y < avctx->height / 2; y++) {
  205. luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0] ];
  206. luma2 = (uint32_t*)&f->data[0][ (y * 2 + 1) * f->linesize[0] ];
  207. cr = (uint32_t*)&f->data[1][ y * f->linesize[1] ];
  208. cb = (uint32_t*)&f->data[2][ y * f->linesize[2] ];
  209. for (x = 0; x < avctx->width; x += 8) {
  210. *luma1++ = *buf32++;
  211. *luma1++ = *buf32++;
  212. *luma2++ = *buf32++;
  213. *luma2++ = *buf32++;
  214. *cr++ = *buf32++;
  215. *cb++ = *buf32++;
  216. }
  217. }
  218. break;
  219. case 1:
  220. /* Fraps v1 is an upside-down BGR24 */
  221. for (y = 0; y<avctx->height; y++)
  222. memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
  223. &buf[y * avctx->width * 3],
  224. 3 * avctx->width);
  225. break;
  226. case 2:
  227. case 4:
  228. /**
  229. * Fraps v2 is Huffman-coded YUV420 planes
  230. * Fraps v4 is virtually the same
  231. */
  232. for (i = 0; i < planes; i++) {
  233. is_chroma = !!i;
  234. if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
  235. avctx->width >> is_chroma,
  236. avctx->height >> is_chroma,
  237. buf + offs[i], offs[i + 1] - offs[i],
  238. is_chroma, 1)) < 0) {
  239. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  240. return ret;
  241. }
  242. }
  243. break;
  244. case 3:
  245. case 5:
  246. /* Virtually the same as version 4, but is for RGB24 */
  247. for (i = 0; i < planes; i++) {
  248. if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
  249. -f->linesize[0], avctx->width, avctx->height,
  250. buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
  251. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  252. return ret;
  253. }
  254. }
  255. out = f->data[0];
  256. // convert pseudo-YUV into real RGB
  257. for (j = 0; j < avctx->height; j++) {
  258. uint8_t *line_end = out + 3*avctx->width;
  259. while (out < line_end) {
  260. out[0] += out[1];
  261. out[2] += out[1];
  262. out += 3;
  263. }
  264. out += f->linesize[0] - 3*avctx->width;
  265. }
  266. break;
  267. }
  268. *got_frame = 1;
  269. return buf_size;
  270. }
  271. /**
  272. * closes decoder
  273. * @param avctx codec context
  274. * @return 0 on success or negative if fails
  275. */
  276. static av_cold int decode_end(AVCodecContext *avctx)
  277. {
  278. FrapsContext *s = (FrapsContext*)avctx->priv_data;
  279. av_freep(&s->tmpbuf);
  280. return 0;
  281. }
  282. AVCodec ff_fraps_decoder = {
  283. .name = "fraps",
  284. .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
  285. .type = AVMEDIA_TYPE_VIDEO,
  286. .id = AV_CODEC_ID_FRAPS,
  287. .priv_data_size = sizeof(FrapsContext),
  288. .init = decode_init,
  289. .close = decode_end,
  290. .decode = decode_frame,
  291. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  292. };