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.

389 lines
13KB

  1. /*
  2. * Fraps FPS1 decoder
  3. * Copyright (c) 2005 Roine Gustafsson
  4. * Copyright (c) 2006 Konstantin Shishkov
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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. #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
  39. #define VLC_BITS 11
  40. /**
  41. * local variable storage
  42. */
  43. typedef struct FrapsContext {
  44. AVCodecContext *avctx;
  45. BswapDSPContext bdsp;
  46. AVFrame *frame;
  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. avctx->pix_fmt = AV_PIX_FMT_NONE; /* set in decode_frame */
  59. s->avctx = avctx;
  60. s->tmpbuf = NULL;
  61. s->frame = av_frame_alloc();
  62. if (!s->frame)
  63. return AVERROR(ENOMEM);
  64. ff_bswapdsp_init(&s->bdsp);
  65. return 0;
  66. }
  67. /**
  68. * Comparator - our nodes should ascend by count
  69. * but with preserved symbol order
  70. */
  71. static int huff_cmp(const void *va, const void *vb)
  72. {
  73. const Node *a = va, *b = vb;
  74. return (a->count - b->count)*256 + a->sym - b->sym;
  75. }
  76. /**
  77. * decode Fraps v2 packed plane
  78. */
  79. static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
  80. int h, const uint8_t *src, int size, int Uoff,
  81. const int step)
  82. {
  83. int i, j, ret;
  84. GetBitContext gb;
  85. VLC vlc;
  86. Node nodes[512];
  87. for (i = 0; i < 256; i++)
  88. nodes[i].count = bytestream_get_le32(&src);
  89. size -= 1024;
  90. if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, VLC_BITS,
  91. nodes, huff_cmp,
  92. FF_HUFFMAN_FLAG_ZERO_COUNT)) < 0)
  93. return ret;
  94. /* we have built Huffman table and are ready to decode plane */
  95. /* convert bits so they may be used by standard bitreader */
  96. s->bdsp.bswap_buf((uint32_t *) s->tmpbuf,
  97. (const uint32_t *) src, size >> 2);
  98. init_get_bits(&gb, s->tmpbuf, size * 8);
  99. for (j = 0; j < h; j++) {
  100. for (i = 0; i < w*step; i += step) {
  101. dst[i] = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
  102. /* lines are stored as deltas between previous lines
  103. * and we need to add 0x80 to the first lines of chroma planes
  104. */
  105. if (j)
  106. dst[i] += dst[i - stride];
  107. else if (Uoff)
  108. dst[i] += 0x80;
  109. if (get_bits_left(&gb) < 0) {
  110. ff_free_vlc(&vlc);
  111. return AVERROR_INVALIDDATA;
  112. }
  113. }
  114. dst += stride;
  115. }
  116. ff_free_vlc(&vlc);
  117. return 0;
  118. }
  119. static int decode_frame(AVCodecContext *avctx,
  120. void *data, int *got_frame,
  121. AVPacket *avpkt)
  122. {
  123. FrapsContext * const s = avctx->priv_data;
  124. const uint8_t *buf = avpkt->data;
  125. int buf_size = avpkt->size;
  126. AVFrame *frame = data;
  127. AVFrame * const f = s->frame;
  128. uint32_t header;
  129. unsigned int version,header_size;
  130. unsigned int x, y;
  131. const uint32_t *buf32;
  132. uint32_t *luma1,*luma2,*cb,*cr;
  133. uint32_t offs[4];
  134. int i, j, ret, is_chroma, planes;
  135. enum AVPixelFormat pix_fmt;
  136. int prev_pic_bit, expected_size;
  137. if (buf_size < 4) {
  138. av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
  139. return AVERROR_INVALIDDATA;
  140. }
  141. header = AV_RL32(buf);
  142. version = header & 0xff;
  143. header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
  144. prev_pic_bit = header & (1U << 31); /* bit 31 means same as previous pic */
  145. if (version > 5) {
  146. av_log(avctx, AV_LOG_ERROR,
  147. "This file is encoded with Fraps version %d. " \
  148. "This codec can only decode versions <= 5.\n", version);
  149. return AVERROR_PATCHWELCOME;
  150. }
  151. buf += 4;
  152. if (header_size == 8)
  153. buf += 4;
  154. pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
  155. if (avctx->pix_fmt != pix_fmt && f->data[0]) {
  156. av_frame_unref(f);
  157. }
  158. avctx->pix_fmt = pix_fmt;
  159. avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED
  160. : AVCOL_RANGE_JPEG;
  161. expected_size = header_size;
  162. switch (version) {
  163. case 0:
  164. default:
  165. /* Fraps v0 is a reordered YUV420 */
  166. if (!prev_pic_bit)
  167. expected_size += avctx->width * avctx->height * 3 / 2;
  168. if (buf_size != expected_size) {
  169. av_log(avctx, AV_LOG_ERROR,
  170. "Invalid frame length %d (should be %d)\n",
  171. buf_size, expected_size);
  172. return AVERROR_INVALIDDATA;
  173. }
  174. if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
  175. av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
  176. avctx->width, avctx->height);
  177. return AVERROR_INVALIDDATA;
  178. }
  179. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  180. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  181. return ret;
  182. }
  183. f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  184. f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
  185. if (f->pict_type == AV_PICTURE_TYPE_I) {
  186. buf32 = (const uint32_t*)buf;
  187. for (y = 0; y < avctx->height / 2; y++) {
  188. luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0]];
  189. luma2 = (uint32_t*)&f->data[0][(y * 2 + 1) * f->linesize[0]];
  190. cr = (uint32_t*)&f->data[1][ y * f->linesize[1]];
  191. cb = (uint32_t*)&f->data[2][ y * f->linesize[2]];
  192. for (x = 0; x < avctx->width; x += 8) {
  193. *(luma1++) = *(buf32++);
  194. *(luma1++) = *(buf32++);
  195. *(luma2++) = *(buf32++);
  196. *(luma2++) = *(buf32++);
  197. *(cr++) = *(buf32++);
  198. *(cb++) = *(buf32++);
  199. }
  200. }
  201. }
  202. break;
  203. case 1:
  204. /* Fraps v1 is an upside-down BGR24 */
  205. if (!prev_pic_bit)
  206. expected_size += avctx->width * avctx->height * 3;
  207. if (buf_size != expected_size) {
  208. av_log(avctx, AV_LOG_ERROR,
  209. "Invalid frame length %d (should be %d)\n",
  210. buf_size, expected_size);
  211. return AVERROR_INVALIDDATA;
  212. }
  213. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  214. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  215. return ret;
  216. }
  217. f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  218. f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
  219. if (f->pict_type == AV_PICTURE_TYPE_I) {
  220. for (y = 0; y<avctx->height; y++)
  221. memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
  222. &buf[y * avctx->width * 3],
  223. 3 * avctx->width);
  224. }
  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. planes = 3;
  233. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  234. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  235. return ret;
  236. }
  237. /* skip frame */
  238. if (buf_size == 8) {
  239. f->pict_type = AV_PICTURE_TYPE_P;
  240. f->key_frame = 0;
  241. break;
  242. }
  243. f->pict_type = AV_PICTURE_TYPE_I;
  244. f->key_frame = 1;
  245. if ((AV_RL32(buf) != FPS_TAG) || (buf_size < (planes * 1024 + 24))) {
  246. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  247. return AVERROR_INVALIDDATA;
  248. }
  249. for (i = 0; i < planes; i++) {
  250. offs[i] = AV_RL32(buf + 4 + i * 4);
  251. if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  252. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  253. return AVERROR_INVALIDDATA;
  254. }
  255. }
  256. offs[planes] = buf_size;
  257. for (i = 0; i < planes; i++) {
  258. is_chroma = !!i;
  259. av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
  260. offs[i + 1] - offs[i] - 1024);
  261. if (!s->tmpbuf)
  262. return AVERROR(ENOMEM);
  263. if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
  264. avctx->width >> is_chroma,
  265. avctx->height >> is_chroma,
  266. buf + offs[i], offs[i + 1] - offs[i],
  267. is_chroma, 1)) < 0) {
  268. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  269. return ret;
  270. }
  271. }
  272. break;
  273. case 3:
  274. case 5:
  275. /* Virtually the same as version 4, but is for RGB24 */
  276. planes = 3;
  277. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  278. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  279. return ret;
  280. }
  281. /* skip frame */
  282. if (buf_size == 8) {
  283. f->pict_type = AV_PICTURE_TYPE_P;
  284. f->key_frame = 0;
  285. break;
  286. }
  287. f->pict_type = AV_PICTURE_TYPE_I;
  288. f->key_frame = 1;
  289. if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
  290. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  291. return AVERROR_INVALIDDATA;
  292. }
  293. for (i = 0; i < planes; i++) {
  294. offs[i] = AV_RL32(buf + 4 + i * 4);
  295. if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  296. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  297. return AVERROR_INVALIDDATA;
  298. }
  299. }
  300. offs[planes] = buf_size;
  301. for (i = 0; i < planes; i++) {
  302. av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
  303. offs[i + 1] - offs[i] - 1024);
  304. if (!s->tmpbuf)
  305. return AVERROR(ENOMEM);
  306. if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
  307. -f->linesize[0], avctx->width, avctx->height,
  308. buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
  309. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  310. return ret;
  311. }
  312. }
  313. // convert pseudo-YUV into real RGB
  314. for (j = 0; j < avctx->height; j++) {
  315. for (i = 0; i < avctx->width; i++) {
  316. f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
  317. f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
  318. }
  319. }
  320. break;
  321. }
  322. if ((ret = av_frame_ref(frame, f)) < 0)
  323. return ret;
  324. *got_frame = 1;
  325. return buf_size;
  326. }
  327. /**
  328. * closes decoder
  329. * @param avctx codec context
  330. * @return 0 on success or negative if fails
  331. */
  332. static av_cold int decode_end(AVCodecContext *avctx)
  333. {
  334. FrapsContext *s = (FrapsContext*)avctx->priv_data;
  335. av_frame_free(&s->frame);
  336. av_freep(&s->tmpbuf);
  337. return 0;
  338. }
  339. AVCodec ff_fraps_decoder = {
  340. .name = "fraps",
  341. .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
  342. .type = AVMEDIA_TYPE_VIDEO,
  343. .id = AV_CODEC_ID_FRAPS,
  344. .priv_data_size = sizeof(FrapsContext),
  345. .init = decode_init,
  346. .close = decode_end,
  347. .decode = decode_frame,
  348. .capabilities = AV_CODEC_CAP_DR1,
  349. };